Skip to main content

ZigZagCodec

Struct ZigZagCodec 

Source
pub struct ZigZagCodec<T, P = NonStrict> { /* private fields */ }
Expand description

Type-level unchecked ZigZag + unsigned LEB128 codec.

§Type Parameters

  • T: Signed integer value type to decode from ZigZag-encoded LEB128 bytes and encode into ZigZag-encoded LEB128 bytes.
  • P: Type-level decoding policy implementing Leb128DecodePolicy for the underlying unsigned LEB128 payload. Use crate::Strict to reject non-canonical inputs, or NonStrict to accept non-canonical inputs.

§Examples

use qubit_codec_binary::{
    NonStrict,
    ZigZagCodec,
};

let mut output = [0_u8; ZigZagCodec::<i64, NonStrict>::MAX_UNITS_PER_VALUE];
let written = unsafe {
    ZigZagCodec::<i64, NonStrict>::encode_unchecked(-42, &mut output, 0)
};
assert_eq!(1, written);

let (decoded, consumed) = unsafe {
    ZigZagCodec::<i64, NonStrict>::decode_unchecked(&output[..written], 0)
}.expect("canonical ZigZag LEB128 value should decode");
assert_eq!(-42, decoded);
assert_eq!(1, consumed.get());

Implementations§

Source§

impl<P> ZigZagCodec<i8, P>

Source

pub const MIN_UNITS_PER_VALUE: usize = 1

Minimum number of bytes that can represent a complete value.

Source

pub const MAX_UNITS_PER_VALUE: usize = Leb128Codec<u8, NonStrict>::MAX_UNITS_PER_VALUE

Maximum number of bytes required to encode or decode this type.

Source

pub unsafe fn decode_unchecked( input: &[u8], index: usize, ) -> Result<(i8, NonZeroUsize), Leb128DecodeError>

Decodes a value from input starting at index without bounds checks.

§Parameters
  • input: Source byte buffer.
  • index: Start index in input.
§Returns

Returns the decoded value and the non-zero number of consumed bytes.

§Errors

Returns Leb128DecodeError if the underlying LEB128 bytes are incomplete, malformed, or non-canonical under strict policy.

§Safety

The caller must guarantee that index is a valid boundary and at least Self::MIN_UNITS_PER_VALUE byte is readable from index.

Source

pub unsafe fn encode_unchecked( value: i8, output: &mut [u8], index: usize, ) -> usize

Encodes value into output starting at index without bounds checks.

§Parameters
  • value: Value to encode.
  • output: Destination byte buffer.
  • index: Start index in output.
§Returns

Returns the number of written bytes.

§Safety

The caller must guarantee that output.as_mut_ptr().add(index) is valid to write Self::MAX_UNITS_PER_VALUE bytes.

Source§

impl<P> ZigZagCodec<i16, P>

Source

pub const MIN_UNITS_PER_VALUE: usize = 1

Minimum number of bytes that can represent a complete value.

Source

pub const MAX_UNITS_PER_VALUE: usize = Leb128Codec<u16, NonStrict>::MAX_UNITS_PER_VALUE

Maximum number of bytes required to encode or decode this type.

Source

pub unsafe fn decode_unchecked( input: &[u8], index: usize, ) -> Result<(i16, NonZeroUsize), Leb128DecodeError>

Decodes a value from input starting at index without bounds checks.

§Parameters
  • input: Source byte buffer.
  • index: Start index in input.
§Returns

Returns the decoded value and the non-zero number of consumed bytes.

§Errors

Returns Leb128DecodeError if the underlying LEB128 bytes are incomplete, malformed, or non-canonical under strict policy.

§Safety

The caller must guarantee that index is a valid boundary and at least Self::MIN_UNITS_PER_VALUE byte is readable from index.

Source

pub unsafe fn encode_unchecked( value: i16, output: &mut [u8], index: usize, ) -> usize

Encodes value into output starting at index without bounds checks.

§Parameters
  • value: Value to encode.
  • output: Destination byte buffer.
  • index: Start index in output.
§Returns

Returns the number of written bytes.

§Safety

The caller must guarantee that output.as_mut_ptr().add(index) is valid to write Self::MAX_UNITS_PER_VALUE bytes.

Source§

impl<P> ZigZagCodec<i32, P>

Source

pub const MIN_UNITS_PER_VALUE: usize = 1

Minimum number of bytes that can represent a complete value.

Source

pub const MAX_UNITS_PER_VALUE: usize = Leb128Codec<u32, NonStrict>::MAX_UNITS_PER_VALUE

Maximum number of bytes required to encode or decode this type.

Source

pub unsafe fn decode_unchecked( input: &[u8], index: usize, ) -> Result<(i32, NonZeroUsize), Leb128DecodeError>

Decodes a value from input starting at index without bounds checks.

§Parameters
  • input: Source byte buffer.
  • index: Start index in input.
§Returns

Returns the decoded value and the non-zero number of consumed bytes.

§Errors

Returns Leb128DecodeError if the underlying LEB128 bytes are incomplete, malformed, or non-canonical under strict policy.

§Safety

The caller must guarantee that index is a valid boundary and at least Self::MIN_UNITS_PER_VALUE byte is readable from index.

Source

pub unsafe fn encode_unchecked( value: i32, output: &mut [u8], index: usize, ) -> usize

Encodes value into output starting at index without bounds checks.

§Parameters
  • value: Value to encode.
  • output: Destination byte buffer.
  • index: Start index in output.
§Returns

Returns the number of written bytes.

§Safety

The caller must guarantee that output.as_mut_ptr().add(index) is valid to write Self::MAX_UNITS_PER_VALUE bytes.

Source§

impl<P> ZigZagCodec<i64, P>

Source

pub const MIN_UNITS_PER_VALUE: usize = 1

Minimum number of bytes that can represent a complete value.

Source

pub const MAX_UNITS_PER_VALUE: usize = Leb128Codec<u64, NonStrict>::MAX_UNITS_PER_VALUE

Maximum number of bytes required to encode or decode this type.

Source

pub unsafe fn decode_unchecked( input: &[u8], index: usize, ) -> Result<(i64, NonZeroUsize), Leb128DecodeError>

Decodes a value from input starting at index without bounds checks.

§Parameters
  • input: Source byte buffer.
  • index: Start index in input.
§Returns

Returns the decoded value and the non-zero number of consumed bytes.

§Errors

Returns Leb128DecodeError if the underlying LEB128 bytes are incomplete, malformed, or non-canonical under strict policy.

§Safety

The caller must guarantee that index is a valid boundary and at least Self::MIN_UNITS_PER_VALUE byte is readable from index.

Source

pub unsafe fn encode_unchecked( value: i64, output: &mut [u8], index: usize, ) -> usize

Encodes value into output starting at index without bounds checks.

§Parameters
  • value: Value to encode.
  • output: Destination byte buffer.
  • index: Start index in output.
§Returns

Returns the number of written bytes.

§Safety

The caller must guarantee that output.as_mut_ptr().add(index) is valid to write Self::MAX_UNITS_PER_VALUE bytes.

Source§

impl<P> ZigZagCodec<i128, P>

Source

pub const MIN_UNITS_PER_VALUE: usize = 1

Minimum number of bytes that can represent a complete value.

Source

pub const MAX_UNITS_PER_VALUE: usize = Leb128Codec<u128, NonStrict>::MAX_UNITS_PER_VALUE

Maximum number of bytes required to encode or decode this type.

Source

pub unsafe fn decode_unchecked( input: &[u8], index: usize, ) -> Result<(i128, NonZeroUsize), Leb128DecodeError>

Decodes a value from input starting at index without bounds checks.

§Parameters
  • input: Source byte buffer.
  • index: Start index in input.
§Returns

Returns the decoded value and the non-zero number of consumed bytes.

§Errors

Returns Leb128DecodeError if the underlying LEB128 bytes are incomplete, malformed, or non-canonical under strict policy.

§Safety

The caller must guarantee that index is a valid boundary and at least Self::MIN_UNITS_PER_VALUE byte is readable from index.

Source

pub unsafe fn encode_unchecked( value: i128, output: &mut [u8], index: usize, ) -> usize

Encodes value into output starting at index without bounds checks.

§Parameters
  • value: Value to encode.
  • output: Destination byte buffer.
  • index: Start index in output.
§Returns

Returns the number of written bytes.

§Safety

The caller must guarantee that output.as_mut_ptr().add(index) is valid to write Self::MAX_UNITS_PER_VALUE bytes.

Source§

impl<P> ZigZagCodec<isize, P>

Source

pub const MIN_UNITS_PER_VALUE: usize = 1

Minimum number of bytes that can represent a complete value.

Source

pub const MAX_UNITS_PER_VALUE: usize = Leb128Codec<usize, NonStrict>::MAX_UNITS_PER_VALUE

Maximum number of bytes required to encode or decode this type.

Source

pub unsafe fn decode_unchecked( input: &[u8], index: usize, ) -> Result<(isize, NonZeroUsize), Leb128DecodeError>

Decodes a value from input starting at index without bounds checks.

§Parameters
  • input: Source byte buffer.
  • index: Start index in input.
§Returns

Returns the decoded value and the non-zero number of consumed bytes.

§Errors

Returns Leb128DecodeError if the underlying LEB128 bytes are incomplete, malformed, or non-canonical under strict policy.

§Safety

The caller must guarantee that index is a valid boundary and at least Self::MIN_UNITS_PER_VALUE byte is readable from index.

Source

pub unsafe fn encode_unchecked( value: isize, output: &mut [u8], index: usize, ) -> usize

Encodes value into output starting at index without bounds checks.

§Parameters
  • value: Value to encode.
  • output: Destination byte buffer.
  • index: Start index in output.
§Returns

Returns the number of written bytes.

§Safety

The caller must guarantee that output.as_mut_ptr().add(index) is valid to write Self::MAX_UNITS_PER_VALUE bytes.

Trait Implementations§

Source§

impl<T: Clone, P: Clone> Clone for ZigZagCodec<T, P>

Source§

fn clone(&self) -> ZigZagCodec<T, P>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P> Codec for ZigZagCodec<i128, P>

Source§

type Value = i128

Logical value decoded from or encoded into the buffer.
Source§

type Unit = u8

Buffer unit used by the encoded representation.
Source§

type DecodeError = Leb128DecodeError

Error reported when decoding malformed units.
Source§

type EncodeError = Infallible

Error reported when encoding an unsupported value.
Source§

fn min_units_per_value(&self) -> NonZeroUsize

Returns the minimum possible unit count for one encoded value. Read more
Source§

fn max_units_per_value(&self) -> NonZeroUsize

Returns the maximum non-zero unit count needed to encode or decode one value. Read more
Source§

unsafe fn decode_unchecked( &self, input: &[u8], index: usize, ) -> Result<(i128, NonZeroUsize), Self::DecodeError>

Decodes one value from input starting at index. Read more
Source§

unsafe fn encode_unchecked( &self, value: &i128, output: &mut [u8], index: usize, ) -> Result<usize, Self::EncodeError>

Encodes one borrowed value into output starting at index. Read more
Source§

impl<P> Codec for ZigZagCodec<i16, P>

Source§

type Value = i16

Logical value decoded from or encoded into the buffer.
Source§

type Unit = u8

Buffer unit used by the encoded representation.
Source§

type DecodeError = Leb128DecodeError

Error reported when decoding malformed units.
Source§

type EncodeError = Infallible

Error reported when encoding an unsupported value.
Source§

fn min_units_per_value(&self) -> NonZeroUsize

Returns the minimum possible unit count for one encoded value. Read more
Source§

fn max_units_per_value(&self) -> NonZeroUsize

Returns the maximum non-zero unit count needed to encode or decode one value. Read more
Source§

unsafe fn decode_unchecked( &self, input: &[u8], index: usize, ) -> Result<(i16, NonZeroUsize), Self::DecodeError>

Decodes one value from input starting at index. Read more
Source§

unsafe fn encode_unchecked( &self, value: &i16, output: &mut [u8], index: usize, ) -> Result<usize, Self::EncodeError>

Encodes one borrowed value into output starting at index. Read more
Source§

impl<P> Codec for ZigZagCodec<i32, P>

Source§

type Value = i32

Logical value decoded from or encoded into the buffer.
Source§

type Unit = u8

Buffer unit used by the encoded representation.
Source§

type DecodeError = Leb128DecodeError

Error reported when decoding malformed units.
Source§

type EncodeError = Infallible

Error reported when encoding an unsupported value.
Source§

fn min_units_per_value(&self) -> NonZeroUsize

Returns the minimum possible unit count for one encoded value. Read more
Source§

fn max_units_per_value(&self) -> NonZeroUsize

Returns the maximum non-zero unit count needed to encode or decode one value. Read more
Source§

unsafe fn decode_unchecked( &self, input: &[u8], index: usize, ) -> Result<(i32, NonZeroUsize), Self::DecodeError>

Decodes one value from input starting at index. Read more
Source§

unsafe fn encode_unchecked( &self, value: &i32, output: &mut [u8], index: usize, ) -> Result<usize, Self::EncodeError>

Encodes one borrowed value into output starting at index. Read more
Source§

impl<P> Codec for ZigZagCodec<i64, P>

Source§

type Value = i64

Logical value decoded from or encoded into the buffer.
Source§

type Unit = u8

Buffer unit used by the encoded representation.
Source§

type DecodeError = Leb128DecodeError

Error reported when decoding malformed units.
Source§

type EncodeError = Infallible

Error reported when encoding an unsupported value.
Source§

fn min_units_per_value(&self) -> NonZeroUsize

Returns the minimum possible unit count for one encoded value. Read more
Source§

fn max_units_per_value(&self) -> NonZeroUsize

Returns the maximum non-zero unit count needed to encode or decode one value. Read more
Source§

unsafe fn decode_unchecked( &self, input: &[u8], index: usize, ) -> Result<(i64, NonZeroUsize), Self::DecodeError>

Decodes one value from input starting at index. Read more
Source§

unsafe fn encode_unchecked( &self, value: &i64, output: &mut [u8], index: usize, ) -> Result<usize, Self::EncodeError>

Encodes one borrowed value into output starting at index. Read more
Source§

impl<P> Codec for ZigZagCodec<i8, P>

Source§

type Value = i8

Logical value decoded from or encoded into the buffer.
Source§

type Unit = u8

Buffer unit used by the encoded representation.
Source§

type DecodeError = Leb128DecodeError

Error reported when decoding malformed units.
Source§

type EncodeError = Infallible

Error reported when encoding an unsupported value.
Source§

fn min_units_per_value(&self) -> NonZeroUsize

Returns the minimum possible unit count for one encoded value. Read more
Source§

fn max_units_per_value(&self) -> NonZeroUsize

Returns the maximum non-zero unit count needed to encode or decode one value. Read more
Source§

unsafe fn decode_unchecked( &self, input: &[u8], index: usize, ) -> Result<(i8, NonZeroUsize), Self::DecodeError>

Decodes one value from input starting at index. Read more
Source§

unsafe fn encode_unchecked( &self, value: &i8, output: &mut [u8], index: usize, ) -> Result<usize, Self::EncodeError>

Encodes one borrowed value into output starting at index. Read more
Source§

impl<P> Codec for ZigZagCodec<isize, P>

Source§

type Value = isize

Logical value decoded from or encoded into the buffer.
Source§

type Unit = u8

Buffer unit used by the encoded representation.
Source§

type DecodeError = Leb128DecodeError

Error reported when decoding malformed units.
Source§

type EncodeError = Infallible

Error reported when encoding an unsupported value.
Source§

fn min_units_per_value(&self) -> NonZeroUsize

Returns the minimum possible unit count for one encoded value. Read more
Source§

fn max_units_per_value(&self) -> NonZeroUsize

Returns the maximum non-zero unit count needed to encode or decode one value. Read more
Source§

unsafe fn decode_unchecked( &self, input: &[u8], index: usize, ) -> Result<(isize, NonZeroUsize), Self::DecodeError>

Decodes one value from input starting at index. Read more
Source§

unsafe fn encode_unchecked( &self, value: &isize, output: &mut [u8], index: usize, ) -> Result<usize, Self::EncodeError>

Encodes one borrowed value into output starting at index. Read more
Source§

impl<T: Copy, P: Copy> Copy for ZigZagCodec<T, P>

Source§

impl<T: Debug, P: Debug> Debug for ZigZagCodec<T, P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default, P: Default> Default for ZigZagCodec<T, P>

Source§

fn default() -> ZigZagCodec<T, P>

Returns the “default value” for a type. Read more
Source§

impl<T: Eq, P: Eq> Eq for ZigZagCodec<T, P>

Source§

impl<T: PartialEq, P: PartialEq> PartialEq for ZigZagCodec<T, P>

Source§

fn eq(&self, other: &ZigZagCodec<T, P>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, P> StructuralPartialEq for ZigZagCodec<T, P>

Auto Trait Implementations§

§

impl<T, P> Freeze for ZigZagCodec<T, P>

§

impl<T, P> RefUnwindSafe for ZigZagCodec<T, P>

§

impl<T, P> Send for ZigZagCodec<T, P>

§

impl<T, P> Sync for ZigZagCodec<T, P>

§

impl<T, P> Unpin for ZigZagCodec<T, P>

§

impl<T, P> UnsafeUnpin for ZigZagCodec<T, P>

§

impl<T, P> UnwindSafe for ZigZagCodec<T, P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.