pub trait SerHex<C>: Sizedwhere
C: HexConf,{
type Error: Error;
// Required methods
fn into_hex_raw<D>(&self, dst: D) -> Result<(), Self::Error>
where D: Write;
fn from_hex_raw<S>(src: S) -> Result<Self, Self::Error>
where S: AsRef<[u8]>;
// Provided methods
fn into_hex(&self) -> Result<String, Self::Error> { ... }
fn from_hex<S>(src: S) -> Result<Self, Self::Error>
where S: AsRef<[u8]> { ... }
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer { ... }
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> { ... }
}
Expand description
Trait specifying custom serialization and deserialization logic from a
hexadecimal string to some arbitrary type. This trait can be used to apply
custom parsing when using serde’s #[derive(Serialize,Deserialize)]
flag. Just add #[serde(with = "SerHex")]
above any fields which implement
this trait. Simplistic default implimentations for the the serialize
and
deserialize
methods are provided based on into_hex_raw
and from_hex_raw
respectively.
Required Associated Types§
Sourcetype Error: Error
type Error: Error
Error type of the implementation.
Unless you have a compelling reason to do so, it is best to use the error
type exposed by serde-hex
, since this is the error used for most provided
implementations (the generic array impls will work with any error that
implements From
for the serde-hex
error type).
Required Methods§
Provided Methods§
Sourcefn into_hex(&self) -> Result<String, Self::Error>
fn into_hex(&self) -> Result<String, Self::Error>
Attempt to convert self
into a hexadecimal string representation.
Sourcefn from_hex<S>(src: S) -> Result<Self, Self::Error>
fn from_hex<S>(src: S) -> Result<Self, Self::Error>
Attempt to convert a slice of hexadecimal bytes into an instance of Self
.
Sourcefn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Attempt to serialize self
into a hexadecimal string representation.
NOTE: The default implementation attempts to avoid heap-allocation with a
SmallVec
of size [u8;64]
. This default will
prevent heap-alloc for non-prefixed serializations of [u8;32]
or smaller.
Sourcefn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Attempt to deserialize a hexadecimal string into an instance of Self
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.