[−][src]Crate hexutil
Implement common traits for binary representable data.
Use the impl_hex
macro to implement the ToHex
, FromHex
, Display
, FromStr
, Serialize
and Deserialize
traits.
This can be done by returning a reference to some bytes:
struct Test([u8; 42]); hexutil::impl_hex!(Test, 42, |&self| &self.0, |data| Ok(Self(data)));
Or by returning some bytes by value:
struct Test(u128); hexutil::impl_hex!(Test, 16, |self| self.0.to_le_bytes(), |data| Ok(Self( u128::from_le_bytes(data) )));
Example
struct Test(u16); hexutil::impl_hex!(Test, 2, |self| self.0.to_le_bytes(), |data| Ok(Self( u16::from_le_bytes(data) ))); let test = Test(0x1234); // std::fmt::Display assert_eq!(format!("{}", test), "3412"); // std::string::ToString let hex = test.to_string(); assert_eq!(hex, "3412"); // std::convert::FromStr let test: Test = hex.parse().unwrap(); assert_eq!(test, Test(0x1234)); // hexutil::ToHex let hex = test.to_hex(); assert_eq!(hex, "3412"); // hexutil::FromHex let test = Test::from_hex(hex.as_bytes()).unwrap(); assert_eq!(test, Test(0x1234)); // hexutil::ParseHex let test: Test = hex.parse_hex().unwrap(); assert_eq!(test, Test(0x1234)); // serde::Serialize (with serializer.is_human_readable() == true) let json = serde_json::to_string(&test).unwrap(); assert_eq!(json, r#""3412""#); // serde::Deserialize (with deserializer.is_human_readable() == true) let test: Test = serde_json::from_str(&json).unwrap(); assert_eq!(test, Test(0x1234)); // serde::Serialize (with serializer.is_human_readable() == false) let bin = bincode::serialize(&test).unwrap(); assert_eq!(bin, [0x34, 0x12]); // serde::Deserialize (with deserializer.is_human_readable() == false) let test: Test = bincode::deserialize(&bin).unwrap(); assert_eq!(test, Test(0x1234));
Presets
You can append a list of presets what do derive:
Name | Desciption |
---|---|
default | convert and serde |
convert | Display and FromStr |
Display | Implement the std::fmt::Display trait (enables the to_string() method) |
FromStr | Implement the std::convert::FromStr trait (enables the str.parse() method) |
serde | Serialize and Deserialize |
Serialize | Implement the serde::Serialize trait |
Deserialize | Implement the serde::Deserialize trait |
Derive only the ToHex
, FromHex
, Serialize
and Deserialize
traits:
struct Test([u8; 42]); hexutil::impl_hex!(Test, 42, |self| self.0, |data| Ok(Self(data)), [serde]);
FromHex
Error
The second function returns a Result<Self, FromHexError>
:
struct Test([u8; 42]); hexutil::impl_hex!(Test, 42, |self| self.0, |data| { Err(FromHexError::CustomStr("can't create this from hex")) });
Or use FromHexError::InvalidValue
to display a default message:
struct Test([u8; 42]); hexutil::impl_hex!(Test, 42, |self| self.0, |data| Err( FromHexError::InvalidValue ));
One direction only
You can also implement only one direction:
struct Test([u8; 42]); hexutil::impl_to_hex!(Test, 42, |self| self.0);
struct Test([u8; 42]); hexutil::impl_from_hex!(Test, 42, |data| Ok(Self(data)));
Modules
unstable | Unstable traits and functions. |
Macros
impl_from_hex | Implement common traits for binary representable data (from hex only). |
impl_hex | Implement common traits for binary representable data. |
impl_to_hex | Implement common traits for binary representable data (to hex only). |
Enums
FromHexError | An error occured while converting from a hexadecimal value. |
Traits
FromHex | A type that can be created from a hexadecimal representation. |
ParseHex | Parse a hexadecimal value. |
ToHex | A type that can be converted to a hexadecimal representation. |