stevenarella icon indicating copy to clipboard operation
stevenarella copied to clipboard

impl From<Packet types> for Packet

Open Itsusinn opened this issue 3 years ago • 3 comments
trafficstars

Without impl Into, I have to write something like

  let pkt = Packet::ClientSettings(
    packet::play::serverbound::ClientSettings {
      locale: "en_GB".to_string(),
      view_distance: 2,
      chat_mode: protocol::VarInt(0),
      chat_colors: false,
      displayed_skin_parts: 0,
      main_hand: protocol::VarInt(0),
    }
  );

This commit makes the following code possible

  let pkt: Packet = packet::play::serverbound::ClientSettings {
      locale: "en_GB".to_string(),
      view_distance: 2,
      chat_mode: protocol::VarInt(0),
      chat_colors: false,
      displayed_skin_parts: 0,
      main_hand: protocol::VarInt(0),
  }.into();

It's more tidy

Itsusinn avatar Jul 18 '22 13:07 Itsusinn

I think it would be better to implement From<$name> for Packet instead of Into<Packet> for $name

mpfaff avatar Jul 19 '22 13:07 mpfaff

I think it would be better to implement From<$name> for Packet instead of Into<Packet> for $name

Thanks for the advice @mpfaff , looks like I made a rookie mistake (

Implementing From automatically provides one with an implementation of Into, but implementing Into don't do the same thing

Itsusinn avatar Jul 19 '22 13:07 Itsusinn

Implementing From automatically provides one with an implementation of Into, but implementing Into don't do the same thing

Hey, no worries! You're contributing to an open source project and from my perspective we're all here to learn.

As a side note, I used to implement Into by default because prior to Rust 1.41 you could not implement From for a type across crate boundaries so Into was the only option in a lot of cases.

mpfaff avatar Jul 19 '22 14:07 mpfaff