Attribute Macro tls_codec::conditionally_deserializable
source · #[conditionally_deserializable]
conditional_deserialization
only.Expand description
The conditionally_deserializable
attribute macro creates two versions of
the affected struct: One that implements the
Deserialize
and
DeserializeBytes
traits and one that
does not. It does so by introducing a boolean const generic that indicates
if the struct can be deserialized or not.
This conditional deserialization can be used, for example, to implement a simple state machine, where after deserialization, the user must first complete an additional transition (e.g. verification) to the undeserializable version of the struct, which might then implement functions for further processing.
For ease of use, the macro creates type aliases for the deserializable and
undeserializable variant of the struct, where the alias is the name of the
struct prefixed with Deserializable
or Undeserializable
respectively.
Due to the way that the macro rewrites the struct, it must be placed before
any #[derive(...)]
statements.
The conditionally_deserializable
attribute macro is only available if the
conditional_deserialization
feature is enabled.
use tls_codec_derive::{TlsSerialize, TlsDeserialize, TlsSize, conditionally_deserializable};
#[conditionally_deserializable(Bytes)]
#[derive(TlsDeserialize, TlsSerialize, TlsSize)]
struct ExampleStruct {
pub a: u16,
}
impl UndeserializableExampleStruct {
#[cfg(feature = "conditional_deserialization")]
fn deserialize(bytes: &[u8]) -> Result<Self, tls_codec::Error> {
Self::tls_deserialize_exact(bytes)
}
}