#[repr(transparent)]pub struct ByteArray(pub Vec<u8>);Expand description
A raw sequence of bytes whose length is supplied by context.
A ByteArray has no wire length prefix. Its meaning and number of bytes
are determined by the enclosing packet or data structure, which supplies
the length through Context::for_array_length or
Context::with_array_length. It is encoded as exactly that many bytes:
byte[0] + byte[1] + ... + byte[length - 1]This differs from PrefixedArray, which stores its own VarInt length,
and from Array, which supports arbitrary contextual element codecs.
ByteArray writes and reads its byte buffer in one operation and does not
use array element contexts.
§Examples
use mcproto_types::ContextualCodec;
use mcproto_types::contextual::{ByteArray, Context};
let value = ByteArray(vec![0xde, 0xad, 0xbe, 0xef]);
let context = Context::for_array_length(4);
let mut encoded = Vec::new();
value.encode_with_context(&mut encoded, &context)?;
assert_eq!(encoded, [0xde, 0xad, 0xbe, 0xef]);
let mut input = encoded.as_slice();
assert_eq!(ByteArray::decode_with_context(&mut input, &context)?, value);
assert!(input.is_empty());Tuple Fields§
§0: Vec<u8>The raw bytes.
Implementations§
Trait Implementations§
Source§impl ContextualCodec for ByteArray
impl ContextualCodec for ByteArray
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 Eq for ByteArray
impl StructuralPartialEq for ByteArray
Auto Trait Implementations§
impl Freeze for ByteArray
impl RefUnwindSafe for ByteArray
impl Send for ByteArray
impl Sync for ByteArray
impl Unpin for ByteArray
impl UnsafeUnpin for ByteArray
impl UnwindSafe for ByteArray
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