Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move convert encoder out of IP addr module. #41

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/derive/convert.rs
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 alloc::vec::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())
}
}
4 changes: 2 additions & 2 deletions src/derive/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ impl<'a, T: Decode<'a>, E: Decode<'a>> Decode<'a> for core::result::Result<T, E>
macro_rules! impl_convert {
($want: path, $have: ty) => {
impl Encode for $want {
type Encoder = super::ip_addr::ConvertIntoEncoder<$have>;
type Encoder = super::convert::ConvertIntoEncoder<$have>;
}
impl<'a> Decode<'a> for $want {
type Decoder = super::ip_addr::ConvertFromDecoder<'a, $have>;
type Decoder = super::convert::ConvertFromDecoder<'a, $have>;
}
};
}
Expand Down
57 changes: 1 addition & 56 deletions src/derive/ip_addr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::coder::{Buffer, Decoder, Encoder, Result, View};
use crate::derive::{Decode, Encode};
use core::num::NonZeroUsize;
use super::convert::ConvertFrom;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

macro_rules! ipvx_addr {
Expand Down Expand Up @@ -94,56 +92,3 @@ impl ConvertFrom<SocketAddrConversion> for SocketAddr {
}
}
}

// 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())
}
}
1 change: 1 addition & 0 deletions src/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloc::vec::Vec;
use core::num::NonZeroUsize;

mod array;
mod convert;
mod duration;
mod empty;
mod impls;
Expand Down
2 changes: 2 additions & 0 deletions src/pack_ints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ mod tests {
fn $name() {
type T = $t;
for increment in [0, 1, u8::MAX as u128 + 1, u16::MAX as u128 + 1, u32::MAX as u128 + 1, u64::MAX as u128 + 1] {
#[allow(irrefutable_let_patterns)]
let Ok(increment) = T::try_from(increment) else {
continue;
};
Expand All @@ -603,6 +604,7 @@ mod tests {
if max == T::MAX as i128 {
continue;
}
#[allow(irrefutable_let_patterns)]
let Ok(start) = T::try_from(max) else {
continue;
};
Expand Down
Loading