From 1eef0a537e13b56d55a5a96e61a7e1361dbb02d6 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 29 Jul 2022 17:17:14 -0500 Subject: simplify switching packet states --- azalea-protocol/src/connect.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'azalea-protocol/src') diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index cf251576..75c64517 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -74,25 +74,11 @@ impl Connection { } pub fn login(self) -> Connection { - Connection { - stream: self.stream, - compression_threshold: self.compression_threshold, - enc_cipher: self.enc_cipher, - dec_cipher: self.dec_cipher, - _reading: PhantomData, - _writing: PhantomData, - } + Connection::from(self) } pub fn status(self) -> Connection { - Connection { - stream: self.stream, - compression_threshold: self.compression_threshold, - enc_cipher: self.enc_cipher, - dec_cipher: self.dec_cipher, - _reading: PhantomData, - _writing: PhantomData, - } + Connection::from(self) } } @@ -118,11 +104,27 @@ impl Connection { } pub fn game(self) -> Connection { + Connection::from(self) + } +} + +// rust doesn't let us implement From because allegedly it conflicts with +// `core`'s "impl From for T" so we do this instead +impl Connection +where + R1: ProtocolPacket + Debug, + W1: ProtocolPacket + Debug, +{ + fn from(connection: Connection) -> Connection + where + R2: ProtocolPacket + Debug, + W2: ProtocolPacket + Debug, + { Connection { - stream: self.stream, - compression_threshold: self.compression_threshold, - enc_cipher: self.enc_cipher, - dec_cipher: self.dec_cipher, + stream: connection.stream, + compression_threshold: connection.compression_threshold, + enc_cipher: connection.enc_cipher, + dec_cipher: connection.dec_cipher, _reading: PhantomData, _writing: PhantomData, } -- cgit v1.2.3