From d517cdff44cb0971a9cfcad99b5eef70b22995c0 Mon Sep 17 00:00:00 2001 From: Sean Wilson Date: Sun, 24 May 2020 00:57:19 -0700 Subject: [PATCH] Replace Into with From for built-in types. https://doc.rust-lang.org/std/convert/trait.Into.html Prior to Rust 1.41, if the destination type was not part of the current crate then you couldn't implement From directly. This will fail to compile in older versions of the language because Rust's orphaning rules used to be a little bit more strict. To bypass this, you could implement Into directly. It is important to understand that Into does not provide a From implementation (as From does with Into). Therefore, you should always try to implement From and then fall back to Into if From can't be implemented. Signed-off-by: Sean Wilson --- src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c28524e..be47833 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,10 +74,10 @@ pub struct BigEndian(T); impl Endian for BigEndian{} macro_rules! impl_for_BigEndian{ ( $t:ident ) => { - impl Into<$t> for BigEndian<$t>{ + impl From> for $t { #[inline] - fn into(self) -> $t{ - $t::from_be(self.0) + fn from(data: BigEndian<$t>) -> $t { + $t::from_be(data.0) } } @@ -116,10 +116,10 @@ pub struct LittleEndian(T); impl Endian for LittleEndian{} macro_rules! impl_for_LittleEndian{ ( $t:ident ) => { - impl Into<$t> for LittleEndian<$t>{ + impl From> for $t { #[inline] - fn into(self) -> $t{ - $t::from_le(self.0) + fn from(data: LittleEndian<$t>) -> $t { + $t::from_le(data.0) } }