Skip to content
Open
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
43 changes: 42 additions & 1 deletion src/readable_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,48 @@ impl< 'a, C > Readable< 'a, C > for core::net::IpAddr where C: Context {
}
}

impl< 'a, C > Readable< 'a, C > for core::time::Duration where C: Context {
impl< 'a, C > Readable< 'a, C > for core::net::SocketAddrV4 where C: Context {
#[inline]
fn read_from< R: Reader< 'a, C > >( reader: &mut R ) -> Result< Self, C::Error > {
Ok( core::net::SocketAddrV4::new( reader.read_value()?, reader.read_value()? ) )
}

#[inline]
fn minimum_bytes_needed() -> usize {
6
}
}

impl< 'a, C > Readable< 'a, C > for core::net::SocketAddrV6 where C: Context {
#[inline]
fn read_from< R: Reader< 'a, C > >( reader: &mut R ) -> Result< Self, C::Error > {
Ok( core::net::SocketAddrV6::new( reader.read_value()?, reader.read_value()?, 0, 0 ) )
}

#[inline]
fn minimum_bytes_needed() -> usize {
18
}
}

impl< 'a, C > Readable< 'a, C > for core::net::SocketAddr where C: Context {
#[inline]
fn read_from< R: Reader< 'a, C > >( reader: &mut R ) -> Result< Self, C::Error > {
let kind = reader.read_u8()?;
match kind {
0 => Ok( core::net::SocketAddr::V4( reader.read_value()? ) ),
1 => Ok( core::net::SocketAddr::V6( reader.read_value()? ) ),
_ => Err( crate::error::error_invalid_enum_variant() )
}
}

#[inline]
fn minimum_bytes_needed() -> usize {
7
}
}

impl< 'a, C > Readable< 'a, C > for std::time::Duration where C: Context {
#[inline]
fn read_from< R: Reader< 'a, C > >( reader: &mut R ) -> Result< Self, C::Error > {
let secs = reader.read_u64()?;
Expand Down
52 changes: 51 additions & 1 deletion src/writable_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,57 @@ impl< C > Writable< C > for core::net::IpAddr where C: Context {
}
}

impl< C > Writable< C > for core::time::Duration where C: Context {
impl< C > Writable< C > for core::net::SocketAddrV4 where C: Context {
#[inline]
fn write_to< W >( &self, writer: &mut W ) -> Result< (), C::Error > where W: ?Sized + Writer< C > {
self.ip().write_to( writer )?;
writer.write_u16( self.port() )
}

#[inline]
fn bytes_needed( &self ) -> Result< usize, C::Error > {
Ok( 6 )
}
}

impl< C > Writable< C > for core::net::SocketAddrV6 where C: Context {
#[inline]
fn write_to< W >( &self, writer: &mut W ) -> Result< (), C::Error > where W: ?Sized + Writer< C > {
self.ip().write_to( writer )?;
writer.write_u16( self.port() )
}

#[inline]
fn bytes_needed( &self ) -> Result< usize, C::Error > {
Ok( 18 )
}
}

impl< C > Writable< C > for core::net::SocketAddr where C: Context {
#[inline]
fn write_to< W >( &self, writer: &mut W ) -> Result< (), C::Error > where W: ?Sized + Writer< C > {
match self {
core::net::SocketAddr::V4( address ) => {
writer.write_u8( 0 )?;
address.write_to( writer )
},
core::net::SocketAddr::V6( address ) => {
writer.write_u8( 1 )?;
address.write_to( writer )
}
}
}

#[inline]
fn bytes_needed( &self ) -> Result< usize, C::Error > {
match self {
core::net::SocketAddr::V4( address ) => Writable::< C >::bytes_needed( address ).map( |count| count + 1 ),
core::net::SocketAddr::V6( address ) => Writable::< C >::bytes_needed( address ).map( |count| count + 1 )
}
}
}

impl< C > Writable< C > for std::time::Duration where C: Context {
#[inline]
fn write_to< W >( &self, writer: &mut W ) -> Result< (), C::Error > where W: ?Sized + Writer< C > {
writer.write_u64( self.as_secs() )?;
Expand Down
24 changes: 24 additions & 0 deletions tests/serialization_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,30 @@ symmetric_tests! {
be = [1, 0x20, 0x01, 0x07, 0x20, 0x15, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0, 0xa1, 0x00],
minimum_bytes = 5
}
socket_addr_v4 for core::net::SocketAddrV4 {
in = core::net::SocketAddrV4::new( core::net::Ipv4Addr::new( 127, 0, 0, 1 ), 33 ),
le = [1, 0, 0, 127, 33, 0],
be = [127, 0, 0, 1, 0, 33],
minimum_bytes = 6
}
socket_addr_v6 for core::net::SocketAddrV6 {
in = core::net::SocketAddrV6::new( core::net::Ipv6Addr::new( 0x2001, 0x720, 0x1500, 0x1, 0, 0, 0, 0xa100 ), 33, 0, 0 ),
le = [0x00, 0xa1, 0, 0, 0, 0, 0, 0, 0x01, 0x00, 0x00, 0x15, 0x20, 0x07, 0x01, 0x20, 33, 0],
be = [0x20, 0x01, 0x07, 0x20, 0x15, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0, 0xa1, 0x00, 0, 33],
minimum_bytes = 18
}
socket_addr_ipv4 for core::net::SocketAddr {
in = core::net::SocketAddr::V4( core::net::SocketAddrV4::new( core::net::Ipv4Addr::new( 127, 0, 0, 1 ), 33 ) ),
le = [0, 1, 0, 0, 127, 33, 0],
be = [0, 127, 0, 0, 1, 0, 33],
minimum_bytes = 7
}
socket_addr_ipv6 for core::net::SocketAddr {
in = core::net::SocketAddr::V6( core::net::SocketAddrV6::new( core::net::Ipv6Addr::new( 0x2001, 0x720, 0x1500, 0x1, 0, 0, 0, 0xa100 ), 33, 0, 0 ) ),
le = [1, 0x00, 0xa1, 0, 0, 0, 0, 0, 0, 0x01, 0x00, 0x00, 0x15, 0x20, 0x07, 0x01, 0x20, 33, 0],
be = [1, 0x20, 0x01, 0x07, 0x20, 0x15, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0, 0xa1, 0x00, 0, 33],
minimum_bytes = 7
}
duration for core::time::Duration {
in = core::time::Duration::new( 1, 2 ),
le = [
Expand Down