From: Jeffrey Czyz Date: Mon, 27 Jan 2020 21:44:36 +0000 (-0800) Subject: Use a tuple struct for MessageType X-Git-Tag: v0.0.12~145^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=0e6b207b97264442a9368d8baf28c6ac047ff481;p=rust-lightning Use a tuple struct for MessageType --- diff --git a/lightning/src/ln/wire.rs b/lightning/src/ln/wire.rs index 9dedf580f..c1a596b0c 100644 --- a/lightning/src/ln/wire.rs +++ b/lightning/src/ln/wire.rs @@ -52,9 +52,7 @@ pub enum Message { /// A number identifying a message to determine how it is encoded on the wire. #[derive(Clone, Copy)] -pub struct MessageType { - number: u16, -} +pub struct MessageType(u16); impl Message { /// Returns the type that was used to decode the message payload. @@ -91,13 +89,13 @@ impl Message { impl MessageType { /// Returns whether the message type is even, indicating both endpoints must support it. pub fn is_even(&self) -> bool { - (self.number & 1) == 0 + (self.0 & 1) == 0 } } impl ::std::fmt::Display for MessageType { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - write!(f, "{}", self.number) + write!(f, "{}", self.0) } } @@ -180,7 +178,7 @@ pub fn read(buffer: &mut R) -> Result { - Ok(Message::Unknown(MessageType { number: message_type })) + Ok(Message::Unknown(MessageType(message_type))) }, } } @@ -209,7 +207,7 @@ pub trait Encode { /// Returns the type identifying the message payload. Convenience method for accessing /// [`TYPE`](TYPE). fn type_id(&self) -> MessageType { - MessageType { number: Self::TYPE } + MessageType(Self::TYPE) } } @@ -358,7 +356,7 @@ mod tests { let mut reader = ::std::io::Cursor::new(buffer); let message = read(&mut reader).unwrap(); match message { - Message::Unknown(MessageType { number: ::std::u16::MAX }) => (), + Message::Unknown(MessageType(::std::u16::MAX)) => (), _ => panic!("Expected message type {}; found: {}", ::std::u16::MAX, message.type_id()), } } @@ -394,13 +392,13 @@ mod tests { #[test] fn is_even_message_type() { - let message = Message::Unknown(MessageType { number: 42 }); + let message = Message::Unknown(MessageType(42)); assert!(message.type_id().is_even()); } #[test] fn is_odd_message_type() { - let message = Message::Unknown(MessageType { number: 43 }); + let message = Message::Unknown(MessageType(43)); assert!(!message.type_id().is_even()); } }