Skip to content

Commit

Permalink
Adding non-nightly decode_owned array default impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Nov 21, 2022
1 parent 2a9bd6c commit 8209c0d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions base/src/decode/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ impl <'a, T: DecodeOwned> Decode<'a> for T {
}
}

#[cfg(not(feature = "nightly"))]
impl<T, const N: usize> DecodeOwned for [T; N]
where
T: DecodeOwned<Output=T> + Debug + Default + Copy,
<T as DecodeOwned>::Error: From<Error> + Debug,
{
type Error = <T as DecodeOwned>::Error;
type Output = [<T as DecodeOwned>::Output; N];

fn decode_owned(buff: &[u8]) -> Result<(Self::Output, usize), Self::Error> {
let mut data: [T; N] = [T::default(); N];

let mut offset = 0;
for value in data.iter_mut() {
let (output, length) = T::decode_owned(&buff[offset..])?;
offset += length;
*value = output;
}

Ok((data, offset))
}
}


/// [`DecodeOwned`] for `[T; N]`s containing [`DecodeOwned`] types
#[cfg(feature = "nightly")]
impl <T, const N: usize> DecodeOwned for [T; N]
Expand Down

0 comments on commit 8209c0d

Please sign in to comment.