Skip to content

Commit

Permalink
Replace Into with From for built-in types.
Browse files Browse the repository at this point in the history
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 <spwilson27@gmail.com>
  • Loading branch information
Sean Wilson committed May 24, 2020
1 parent ff964b0 commit 7e18fa7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ pub struct BigEndian<T>(T);
impl<T> Endian<T> for BigEndian<T>{}
macro_rules! impl_for_BigEndian{
( $t:ident ) => {
impl Into<$t> for BigEndian<$t>{
impl From<BigEndian<$t>> for $t {
#[inline]
fn into(self) -> $t{
$t::from_be(self.0)
fn from(data: BigEndian<$t>) -> $t {
$t::from_be(data.0)
}
}

Expand Down Expand Up @@ -116,10 +116,10 @@ pub struct LittleEndian<T>(T);
impl<T> Endian<T> for LittleEndian<T>{}
macro_rules! impl_for_LittleEndian{
( $t:ident ) => {
impl Into<$t> for LittleEndian<$t>{
impl From<LittleEndian<$t>> for $t {
#[inline]
fn into(self) -> $t{
$t::from_le(self.0)
fn from(data: LittleEndian<$t>) -> $t {
$t::from_be(data.0)
}
}

Expand Down

0 comments on commit 7e18fa7

Please sign in to comment.