pub enum IdOr<T> {
Id(i32),
Inline(T),
}Expand description
A protocol value represented either by registry ID or by an inline T.
The Minecraft protocol ID or X wire representation begins with a
VarInt selector:
0means that a value of typeTfollows inline;- a positive value
nrefers to registry IDn - 1and has no inline payload.
VarInt(0) + T // Inline value
VarInt(registry_id + 1) // Registry referenceRegistry IDs held by this type are the actual zero-based IDs, not their
incremented wire selectors. Valid IDs range from 0 through
i32::MAX - 1. The registry itself is implied by the enclosing packet or
field definition; it does not need to be stored in Context because it
does not affect this value’s byte layout.
§Examples
use mcproto_types::{TypeCodec, basic::UnsignedByte};
use mcproto_types::contextual::IdOr;
let inline = IdOr::inline(UnsignedByte(0xab));
let mut encoded = Vec::new();
inline.encode(&mut encoded)?;
assert_eq!(encoded, [0x00, 0xab]);
let mut input = encoded.as_slice();
assert_eq!(IdOr::<UnsignedByte>::decode(&mut input)?, inline);
let reference = IdOr::<UnsignedByte>::id(4);
let mut encoded = Vec::new();
reference.encode(&mut encoded)?;
assert_eq!(encoded, [0x05]);Variants§
Id(i32)
A zero-based ID in the registry implied by the enclosing field.
Inline(T)
A complete value encoded inline after a zero selector.
Implementations§
Source§impl<T> IdOr<T>
impl<T> IdOr<T>
Sourcepub const fn registry_id(&self) -> Option<i32>
pub const fn registry_id(&self) -> Option<i32>
Returns the zero-based registry ID, if this is a reference.
Sourcepub const fn inline_value(&self) -> Option<&T>
pub const fn inline_value(&self) -> Option<&T>
Returns the inline value by reference, if present.
Sourcepub const fn as_ref(&self) -> IdOr<&T>
pub const fn as_ref(&self) -> IdOr<&T>
Borrows the inline value while preserving registry references.
Sourcepub fn into_inline(self) -> Option<T>
pub fn into_inline(self) -> Option<T>
Extracts the inline value, if present.