Skip to content

Commit d739cc7

Browse files
committed
Change Lengthable trait method names to into_len/from_len
std::convert::From<usize> 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`.
1 parent 421497d commit d739cc7

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

protocol/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ impl Serializable for UUID {
446446

447447

448448
pub trait Lengthable : Serializable + Copy + Default {
449-
fn into(self) -> usize;
450-
fn from(_: usize) -> Self;
449+
fn into_len(self) -> usize;
450+
fn from_len(_: usize) -> Self;
451451
}
452452

453453
pub struct LenPrefixed<L: Lengthable, V> {
@@ -467,7 +467,7 @@ impl <L: Lengthable, V: Default> LenPrefixed<L, V> {
467467
impl <L: Lengthable, V: Serializable> Serializable for LenPrefixed<L, V> {
468468
fn read_from<R: io::Read>(buf: &mut R) -> Result<LenPrefixed<L, V>, Error> {
469469
let len_data: L = Serializable::read_from(buf)?;
470-
let len: usize = len_data.into();
470+
let len: usize = len_data.into_len();
471471
let mut data: Vec<V> = Vec::with_capacity(len);
472472
for _ in 0..len {
473473
data.push(Serializable::read_from(buf)?);
@@ -479,7 +479,7 @@ impl <L: Lengthable, V: Serializable> Serializable for LenPrefixed<L, V> {
479479
}
480480

481481
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
482-
let len_data: L = L::from(self.data.len());
482+
let len_data: L = L::from_len(self.data.len());
483483
len_data.write_to(buf)?;
484484
let data = &self.data;
485485
for val in data {
@@ -523,7 +523,7 @@ impl <L: Lengthable> LenPrefixedBytes<L> {
523523
impl <L: Lengthable> Serializable for LenPrefixedBytes<L> {
524524
fn read_from<R: io::Read>(buf: &mut R) -> Result<LenPrefixedBytes<L>, Error> {
525525
let len_data: L = Serializable::read_from(buf)?;
526-
let len: usize = len_data.into();
526+
let len: usize = len_data.into_len();
527527
let mut data: Vec<u8> = Vec::with_capacity(len);
528528
buf.take(len as u64).read_to_end(&mut data)?;
529529
Result::Ok(LenPrefixedBytes {
@@ -533,7 +533,7 @@ impl <L: Lengthable> Serializable for LenPrefixedBytes<L> {
533533
}
534534

535535
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
536-
let len_data: L = L::from(self.data.len());
536+
let len_data: L = L::from_len(self.data.len());
537537
len_data.write_to(buf)?;
538538
buf.write_all(&self.data[..])?;
539539
Result::Ok(())
@@ -557,42 +557,42 @@ impl <L: Lengthable> fmt::Debug for LenPrefixedBytes<L> {
557557
}
558558

559559
impl Lengthable for bool {
560-
fn into(self) -> usize {
560+
fn into_len(self) -> usize {
561561
if self { 1 } else { 0 }
562562
}
563563

564-
fn from(u: usize) -> bool {
564+
fn from_len(u: usize) -> bool {
565565
u != 0
566566
}
567567
}
568568

569569

570570
impl Lengthable for u8 {
571-
fn into(self) -> usize {
571+
fn into_len(self) -> usize {
572572
self as usize
573573
}
574574

575-
fn from(u: usize) -> u8 {
575+
fn from_len(u: usize) -> u8 {
576576
u as u8
577577
}
578578
}
579579

580580
impl Lengthable for i16 {
581-
fn into(self) -> usize {
581+
fn into_len(self) -> usize {
582582
self as usize
583583
}
584584

585-
fn from(u: usize) -> i16 {
585+
fn from_len(u: usize) -> i16 {
586586
u as i16
587587
}
588588
}
589589

590590
impl Lengthable for i32 {
591-
fn into(self) -> usize {
591+
fn into_len(self) -> usize {
592592
self as usize
593593
}
594594

595-
fn from(u: usize) -> i32 {
595+
fn from_len(u: usize) -> i32 {
596596
u as i32
597597
}
598598
}
@@ -603,11 +603,11 @@ impl Lengthable for i32 {
603603
pub struct VarInt(pub i32);
604604

605605
impl Lengthable for VarInt {
606-
fn into(self) -> usize {
606+
fn into_len(self) -> usize {
607607
self.0 as usize
608608
}
609609

610-
fn from(u: usize) -> VarInt {
610+
fn from_len(u: usize) -> VarInt {
611611
VarInt(u as i32)
612612
}
613613
}
@@ -666,11 +666,11 @@ impl fmt::Debug for VarInt {
666666
pub struct VarShort(pub i32);
667667

668668
impl Lengthable for VarShort {
669-
fn into(self) -> usize {
669+
fn into_len(self) -> usize {
670670
self.0 as usize
671671
}
672672

673-
fn from(u: usize) -> VarShort {
673+
fn from_len(u: usize) -> VarShort {
674674
VarShort(u as i32)
675675
}
676676
}
@@ -725,11 +725,11 @@ impl fmt::Debug for VarShort {
725725
pub struct VarLong(pub i64);
726726

727727
impl Lengthable for VarLong {
728-
fn into(self) -> usize {
728+
fn into_len(self) -> usize {
729729
self.0 as usize
730730
}
731731

732-
fn from(u: usize) -> VarLong {
732+
fn from_len(u: usize) -> VarLong {
733733
VarLong(u as i64)
734734
}
735735
}

0 commit comments

Comments
 (0)