From 87bfb8d2a43abbc7bddb45d8cac7e3b0cca5ea1f Mon Sep 17 00:00:00 2001 From: ice_iix Date: Sat, 11 May 2019 13:03:24 -0700 Subject: [PATCH] Change Lengthable trait method names to into_len/from_len std::convert::From cannot be used here because we cannot implement bool<->usize conversions, due to Rust's orphan rules: http://smallcultfollowing.com/babysteps/blog/2015/01/14/little-orphan-impls/ "prevent you from implementing external traits for external types" Nonetheless, Lengthable used the same methods as From. This is allowed but can require disambiguation if both are used, no longer strictly needed for #140 but to reduce confusion and improve clarity, renamed `from` to `from_len` and `into` to `into_len`. --- src/protocol/mod.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index ecd3402..4ded3fc 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -446,8 +446,8 @@ impl Serializable for UUID { pub trait Lengthable : Serializable + Copy + Default { - fn into(self) -> usize; - fn from(_: usize) -> Self; + fn into_len(self) -> usize; + fn from_len(_: usize) -> Self; } pub struct LenPrefixed { @@ -467,7 +467,7 @@ impl LenPrefixed { impl Serializable for LenPrefixed { fn read_from(buf: &mut R) -> Result, Error> { let len_data: L = Serializable::read_from(buf)?; - let len: usize = len_data.into(); + let len: usize = len_data.into_len(); let mut data: Vec = Vec::with_capacity(len); for _ in 0..len { data.push(Serializable::read_from(buf)?); @@ -479,7 +479,7 @@ impl Serializable for LenPrefixed { } fn write_to(&self, buf: &mut W) -> Result<(), Error> { - let len_data: L = L::from(self.data.len()); + let len_data: L = L::from_len(self.data.len()); len_data.write_to(buf)?; let data = &self.data; for val in data { @@ -523,7 +523,7 @@ impl LenPrefixedBytes { impl Serializable for LenPrefixedBytes { fn read_from(buf: &mut R) -> Result, Error> { let len_data: L = Serializable::read_from(buf)?; - let len: usize = len_data.into(); + let len: usize = len_data.into_len(); let mut data: Vec = Vec::with_capacity(len); buf.take(len as u64).read_to_end(&mut data)?; Result::Ok(LenPrefixedBytes { @@ -533,7 +533,7 @@ impl Serializable for LenPrefixedBytes { } fn write_to(&self, buf: &mut W) -> Result<(), Error> { - let len_data: L = L::from(self.data.len()); + let len_data: L = L::from_len(self.data.len()); len_data.write_to(buf)?; buf.write_all(&self.data[..])?; Result::Ok(()) @@ -557,42 +557,42 @@ impl fmt::Debug for LenPrefixedBytes { } impl Lengthable for bool { - fn into(self) -> usize { + fn into_len(self) -> usize { if self { 1 } else { 0 } } - fn from(u: usize) -> bool { + fn from_len(u: usize) -> bool { u != 0 } } impl Lengthable for u8 { - fn into(self) -> usize { + fn into_len(self) -> usize { self as usize } - fn from(u: usize) -> u8 { + fn from_len(u: usize) -> u8 { u as u8 } } impl Lengthable for i16 { - fn into(self) -> usize { + fn into_len(self) -> usize { self as usize } - fn from(u: usize) -> i16 { + fn from_len(u: usize) -> i16 { u as i16 } } impl Lengthable for i32 { - fn into(self) -> usize { + fn into_len(self) -> usize { self as usize } - fn from(u: usize) -> i32 { + fn from_len(u: usize) -> i32 { u as i32 } } @@ -603,11 +603,11 @@ impl Lengthable for i32 { pub struct VarInt(pub i32); impl Lengthable for VarInt { - fn into(self) -> usize { + fn into_len(self) -> usize { self.0 as usize } - fn from(u: usize) -> VarInt { + fn from_len(u: usize) -> VarInt { VarInt(u as i32) } } @@ -666,11 +666,11 @@ impl fmt::Debug for VarInt { pub struct VarShort(pub i32); impl Lengthable for VarShort { - fn into(self) -> usize { + fn into_len(self) -> usize { self.0 as usize } - fn from(u: usize) -> VarShort { + fn from_len(u: usize) -> VarShort { VarShort(u as i32) } } @@ -725,11 +725,11 @@ impl fmt::Debug for VarShort { pub struct VarLong(pub i64); impl Lengthable for VarLong { - fn into(self) -> usize { + fn into_len(self) -> usize { self.0 as usize } - fn from(u: usize) -> VarLong { + fn from_len(u: usize) -> VarLong { VarLong(u as i64) } }