pub trait Hex<T> {
type Error: Display;
// Required methods
fn create_bytes(value: &T) -> Cow<'_, [u8]>;
fn from_bytes(bytes: &[u8]) -> Result<T, Self::Error>;
// Provided methods
fn serialize<S: Serializer>(
value: &T,
serializer: S,
) -> Result<S::Ok, S::Error> { ... }
fn deserialize<'de, D>(deserializer: D) -> Result<T, D::Error>
where D: Deserializer<'de> { ... }
}
Expand description
Provides hex-encoded (de)serialization for serde
.
Note that the trait is automatically implemented for types that
implement AsRef
<[u8]>
and TryFrom
<&[u8]>
.
§Examples
See the crate-level docs for the examples of usage.
Required Associated Types§
Required Methods§
Sourcefn create_bytes(value: &T) -> Cow<'_, [u8]>
fn create_bytes(value: &T) -> Cow<'_, [u8]>
Converts the value into bytes. This is used for serialization.
The returned buffer can be either borrowed from the type, or created by the method.
Provided Methods§
Sourcefn serialize<S: Serializer>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
fn serialize<S: Serializer>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
Serializes the value for serde
. This method is not meant to be overridden.
The serialization is a lower-case hex string
for human-readable serializers (e.g., JSON or TOML), and the original bytes
returned by Self::create_bytes()
for non-human-readable ones.
Sourcefn deserialize<'de, D>(deserializer: D) -> Result<T, D::Error>where
D: Deserializer<'de>,
fn deserialize<'de, D>(deserializer: D) -> Result<T, D::Error>where
D: Deserializer<'de>,
Deserializes a value using serde
. This method is not meant to be overridden.
If the deserializer is human-readable (e.g., JSON or TOML), this method expects a hex-encoded string. Otherwise, the method expects a byte array.
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.