-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::coder::{Buffer, Decoder, Encoder, Result, View}; | ||
use crate::derive::{Decode, Encode}; | ||
use core::num::NonZeroUsize; | ||
|
||
// Like [`From`] but we can implement it ourselves. | ||
pub(crate) trait ConvertFrom<T>: Sized { | ||
fn convert_from(value: T) -> Self; | ||
} | ||
|
||
pub struct ConvertIntoEncoder<T: Encode>(T::Encoder); | ||
|
||
// Can't derive since it would bound T: Default. | ||
impl<T: Encode> Default for ConvertIntoEncoder<T> { | ||
fn default() -> Self { | ||
Self(Default::default()) | ||
} | ||
} | ||
|
||
impl<D, T: Encode + for<'a> ConvertFrom<&'a D>> Encoder<D> for ConvertIntoEncoder<T> { | ||
#[inline(always)] | ||
fn encode(&mut self, t: &D) { | ||
self.0.encode(&T::convert_from(t)); | ||
} | ||
} | ||
|
||
impl<T: Encode> Buffer for ConvertIntoEncoder<T> { | ||
fn collect_into(&mut self, out: &mut Vec<u8>) { | ||
self.0.collect_into(out); | ||
} | ||
fn reserve(&mut self, additional: NonZeroUsize) { | ||
self.0.reserve(additional); | ||
} | ||
} | ||
|
||
/// Decodes a `T` and then converts it with [`ConvertFrom`]. | ||
pub struct ConvertFromDecoder<'a, T: Decode<'a>>(T::Decoder); | ||
|
||
// Can't derive since it would bound T: Default. | ||
impl<'a, T: Decode<'a>> Default for ConvertFromDecoder<'a, T> { | ||
fn default() -> Self { | ||
Self(Default::default()) | ||
} | ||
} | ||
|
||
impl<'a, T: Decode<'a>> View<'a> for ConvertFromDecoder<'a, T> { | ||
fn populate(&mut self, input: &mut &'a [u8], length: usize) -> Result<()> { | ||
self.0.populate(input, length) | ||
} | ||
} | ||
|
||
impl<'a, F: ConvertFrom<T>, T: Decode<'a>> Decoder<'a, F> for ConvertFromDecoder<'a, T> { | ||
#[inline(always)] | ||
fn decode(&mut self) -> F { | ||
F::convert_from(self.0.decode()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters