pub struct Context { /* private fields */ }Expand description
External information required to encode or decode a contextual value.
The context can record whether the current field is present, the length of
an array field, and child contexts for array elements. This supports
protocol fields described as Optional X or Array of X, where the
required information is known from the enclosing packet or data structure.
A Context does not consume or produce any bytes. The enclosing codec must
derive it from already-known protocol state and pass it to
ContextualCodec.
§Examples
use mcproto_types::contextual::Context;
let has_signature = true; // Derived from an earlier packet field.
let context = Context::new(has_signature);
assert!(context.is_present());
assert!(!Context::absent().is_present());Implementations§
Source§impl Context
impl Context
Sourcepub const fn new(present: bool) -> Self
pub const fn new(present: bool) -> Self
Creates a context from a presence condition determined by the enclosing protocol structure.
Sourcepub const fn absent() -> Self
pub const fn absent() -> Self
Creates context for a field that occupies zero bytes on the wire.
Sourcepub const fn for_array_length(length: usize) -> Self
pub const fn for_array_length(length: usize) -> Self
Creates context containing the number of elements in an array field.
Sourcepub fn with_array_length(self, length: usize) -> Self
pub fn with_array_length(self, length: usize) -> Self
Adds an array length to this context, preserving any presence state.
Sourcepub fn with_element_contexts(
self,
contexts: impl IntoIterator<Item = Context>,
) -> Self
pub fn with_element_contexts( self, contexts: impl IntoIterator<Item = Context>, ) -> Self
Adds a child context for each array element.
When child contexts are not supplied, an array passes its own context to every element. Supplying child contexts is required when different elements have different contextual metadata or when arrays are nested.
Sourcepub const fn presence(&self) -> Option<bool>
pub const fn presence(&self) -> Option<bool>
Returns the explicitly supplied presence state, if one exists.
Sourcepub const fn array_length(&self) -> Option<usize>
pub const fn array_length(&self) -> Option<usize>
Returns the contextual array length, if one exists.
Sourcepub const fn is_present(&self) -> bool
pub const fn is_present(&self) -> bool
Returns whether the contextual field is present on the wire.