#[repr(transparent)]pub struct Optional<T>(pub Option<T>);Expand description
A context-controlled optional value of protocol type T.
Optional<T> stores an Option<T>, but it does not encode a presence
marker. When the supplied Context is present, the inner T is encoded
using its ContextualCodec implementation. When the context is absent,
the value occupies zero bytes.
The caller must derive the context from the enclosing packet or data
structure. This type therefore implements ContextualCodec, not
TypeCodec. A value/context mismatch is reported as an encoding error;
it is never silently discarded.
§Examples
use mcproto_types::{ContextualCodec, TypeCodec, basic::UnsignedByte};
use mcproto_types::contextual::{Context, Optional};
let value = Optional::some(UnsignedByte(0xab));
let mut encoded = Vec::new();
value.encode_with_context(&mut encoded, &Context::present())?;
assert_eq!(encoded, [0xab]);
let mut input = encoded.as_slice();
assert_eq!(
Optional::<UnsignedByte>::decode_with_context(&mut input, &Context::present())?,
value,
);Tuple Fields§
§0: Option<T>The optional value held in memory.
Implementations§
Trait Implementations§
Source§impl<T> ContextualCodec for Optional<T>where
T: ContextualCodec,
impl<T> ContextualCodec for Optional<T>where
T: ContextualCodec,
Source§fn encode_with_context(
&self,
writer: &mut impl Write,
context: &Context,
) -> Result<(), CodecError>
fn encode_with_context( &self, writer: &mut impl Write, context: &Context, ) -> Result<(), CodecError>
Encodes this value using context supplied by its enclosing structure.
Source§fn decode_with_context(
reader: &mut impl Read,
context: &Context,
) -> Result<Self, CodecError>
fn decode_with_context( reader: &mut impl Read, context: &Context, ) -> Result<Self, CodecError>
Decodes this value using context supplied by its enclosing structure.
impl<T: Eq> Eq for Optional<T>
Source§impl<T> From<Optional<T>> for PrefixedOptional<T>
impl<T> From<Optional<T>> for PrefixedOptional<T>
impl<T: PartialEq> StructuralPartialEq for Optional<T>
Auto Trait Implementations§
impl<T> Freeze for Optional<T>where
T: Freeze,
impl<T> RefUnwindSafe for Optional<T>where
T: RefUnwindSafe,
impl<T> Send for Optional<T>where
T: Send,
impl<T> Sync for Optional<T>where
T: Sync,
impl<T> Unpin for Optional<T>where
T: Unpin,
impl<T> UnsafeUnpin for Optional<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Optional<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more