pub struct Primitive<'a, S: 'a> { /* private fields */ }
Expand description

The content octets of a primitive value.

You will receive a reference to a value of this type through a closure, possibly wrapped in a Content value. Your task will be to read out all the octets of the value before returning from the closure or produce an error if the value isn’t correctly encoded. If you read less octets than are available, whoever called the closure will produce an error after you returned. Thus, you can read as many octets as you expect and not bother to check whether that was all available octets.

The most basic way to do this is through the primitive’s implementation of the Source trait. Thus, you can gain access to some or all of the octets and mark them read by advancing over them. You can safely attempt to read more octets than available as that will reliably result in a malformed error.

A number of methods are available to deal with the encodings defined for various types. These are prefixed by to_ to indicate that they are intended to convert the content to a certain type. They all read exactly one encoded value.

The value provides access to the decoding mode via the mode method. All methodes that decode data will honour the decoding mode and enforce that data is encoded according to the mode.

Implementations

Returns the current decoding mode.

The higher-level to_ methods will use this mode to enforce that data is encoded correctly.

Sets the current decoding mode.

Produces a content error at the current source position.

Parses the primitive value as a BOOLEAN value.

Parses the primitive value as an INTEGER limited to a i8.

Parses the primitive value as an INTEGER limited to a i8.

Parses the primitive value as an INTEGER limited to a i8.

Parses the primitive value as an INTEGER limited to a i8.

Parses the primitive value as an INTEGER limited to a i8.

Parses the primitive value as an INTEGER limited to a u8.

Parses the primitive value as an INTEGER limited to a u16.

Parses the primitive value as an INTEGER limited to a u32.

Parses the primitive value as a INTEGER value limited to a u64.

Parses the primitive value as a INTEGER value limited to a u128.

Converts the content octets to a NULL value.

Since such a value is empty, this doesn’t really do anything.

Low-level Access

For basic low-level access, Primitive implements the Source trait. Because the length of the content is guaranteed to be known, it can provide a few additional methods. Note that these may still fail because the underlying source doesn’t guarantee that as many octets are actually available.

Returns the number of remaining octets.

The returned value reflects what is left of the expected length of content and therefore decreases when the primitive is advanced.

Skips the rest of the content.

Returns a malformed error if the source ends before the expected length of content.

Returns the remainder of the content as a Bytes value.

Returns a bytes slice of the remainder of the content.

Decode a bytes slice via a closure.

This method can be used in testing code for decoding primitive values by providing a bytes slice with the content. For instance, decoding the to_bool method could be tested like this:

use bcder::Mode;
use bcder::decode::Primitive;

assert_eq!(
    Primitive::decode_slice(
        b"\x00".as_ref(), Mode::Der,
        |prim| prim.to_bool()
    ).unwrap(),
    false
)

Trait Implementations

The error produced when the source failed to read more data.

Returns the current logical postion within the sequence of data.

Request at least len bytes to be available. Read more

Returns a bytes slice with the available data. Read more

Produces a Bytes value from part of the data. Read more

Advance the source by len bytes. Read more

Skip over the next len bytes. Read more

Takes a single octet from the source. Read more

Takes an optional octet from the source. Read more

Returns a content error at the current position of the source.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.