Skip to main content

EncodeValue

Trait EncodeValue 

Source
pub trait EncodeValue {
    // Required methods
    fn value_len(&self) -> Result<Length>;
    fn encode_value(&self, writer: &mut impl Writer) -> Result<()>;

    // Provided method
    fn header(&self) -> Result<Header>
       where Self: Tagged { ... }
}
Expand description

Encode the value part of a Tag-Length-Value encoded field, sans the Tag and Length.

As opposed to Encode, implementer is expected to write the inner content only, without the Header.

When EncodeValue is paired with FixedTag, it produces a complete TLV ASN.1 DER encoding as Encode trait.

§Example

use der::{Encode, EncodeValue, ErrorKind, FixedTag, Length, Tag, Writer};

/// 1-byte month
struct MyByteMonth(u8);

impl EncodeValue for MyByteMonth {

    fn value_len(&self) -> der::Result<Length> {
        Ok(Length::new(1))
    }

    fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
        writer.write_byte(self.0)?;
        Ok(())
    }
}

impl FixedTag for MyByteMonth {
    const TAG: Tag = Tag::OctetString;
}

let month = MyByteMonth(9);
let mut buf = [0u8; 16];
let month_der = month.encode_to_slice(&mut buf).expect("month to encode");

assert_eq!(month_der, b"\x04\x01\x09");

Required Methods§

Source

fn value_len(&self) -> Result<Length>

Compute the length of this value (sans Tag+Length header) when encoded as ASN.1 DER.

§Errors

Returns an error if the value length could not be computed (e.g. overflow).

Source

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Encode value (sans Tag+Length header) as ASN.1 DER using the provided Writer.

§Errors

In the event an encoding error occurred.

Provided Methods§

Source

fn header(&self) -> Result<Header>
where Self: Tagged,

Get the Header used to encode this value.

§Errors

Returns an error if the header could not be computed.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl EncodeValue for bool

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for f64

Available on crate feature real only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for i8

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for i16

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for i32

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for i64

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for i128

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for str

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for u8

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for u16

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for u32

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for u64

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for u128

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for ()

Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, _writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for String

Available on crate feature alloc only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for SystemTime

Available on crate feature std only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for Bytes

Available on crate feature bytes only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl EncodeValue for PrimitiveDateTime

Available on crate feature time only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl<T> EncodeValue for Cow<'_, T>
where T: ToOwned + ?Sized, for<'a> &'a T: EncodeValue,

Available on crate feature alloc only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl<T> EncodeValue for Box<T>
where T: EncodeValue,

Available on crate feature alloc only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Source§

impl<T> EncodeValue for Vec<T>
where T: Encode,

Available on crate feature alloc only.
Source§

fn value_len(&self) -> Result<Length, Error>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<(), Error>

Source§

impl<T, const N: usize> EncodeValue for [T; N]
where T: Encode,

Source§

fn value_len(&self) -> Result<Length, Error>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<(), Error>

Source§

impl<T: Encode> EncodeValue for [T]

Source§

fn value_len(&self) -> Result<Length, Error>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<(), Error>

Source§

impl<T: Flags> EncodeValue for FlagSet<T>
where T::Type: From<bool> + Shl<usize, Output = T::Type>, u128: From<T::Type>,

Available on crate feature flagset only.
Source§

fn value_len(&self) -> Result<Length>

Source§

fn encode_value(&self, writer: &mut impl Writer) -> Result<()>

Implementors§

Source§

impl EncodeValue for &OctetStringRef

Source§

impl EncodeValue for Any

Available on crate feature alloc only.
Source§

impl EncodeValue for AnyRef<'_>

Source§

impl EncodeValue for BitString

Available on crate feature alloc only.
Source§

impl EncodeValue for BitStringRef<'_>

Source§

impl EncodeValue for BmpString

Available on crate feature alloc only.
Source§

impl EncodeValue for GeneralStringRef<'_>

Source§

impl EncodeValue for GeneralizedTime

Source§

impl EncodeValue for Ia5String

Available on crate feature alloc only.
Source§

impl EncodeValue for Int

Available on crate feature alloc only.
Source§

impl EncodeValue for IntRef<'_>

Source§

impl EncodeValue for Null

Source§

impl EncodeValue for OctetString

Available on crate feature alloc only.
Source§

impl EncodeValue for PrintableString

Available on crate feature alloc only.
Source§

impl EncodeValue for SequenceRef

Source§

impl EncodeValue for TeletexString

Available on crate feature alloc only.
Source§

impl EncodeValue for Uint

Available on crate feature alloc only.
Source§

impl EncodeValue for UintRef<'_>

Source§

impl EncodeValue for UtcTime

Source§

impl EncodeValue for DateTime

Source§

impl<'a> EncodeValue for Ia5StringRef<'a>

Source§

impl<'a> EncodeValue for PrintableStringRef<'a>

Source§

impl<'a> EncodeValue for TeletexStringRef<'a>

Source§

impl<'a> EncodeValue for Utf8StringRef<'a>

Source§

impl<'a> EncodeValue for VideotexStringRef<'a>

Source§

impl<T> EncodeValue for Application<T>
where T: EncodeValue + Tagged,

Source§

impl<T> EncodeValue for ApplicationRef<'_, T>
where T: EncodeValue + Tagged,

Source§

impl<T> EncodeValue for ContextSpecific<T>
where T: EncodeValue + Tagged,

Source§

impl<T> EncodeValue for ContextSpecificRef<'_, T>
where T: EncodeValue + Tagged,

Source§

impl<T> EncodeValue for Private<T>
where T: EncodeValue + Tagged,

Source§

impl<T> EncodeValue for PrivateRef<'_, T>
where T: EncodeValue + Tagged,

Source§

impl<T> EncodeValue for SetOfVec<T>
where T: Encode + DerOrd,

Available on (crate features alloc or heapless) and crate feature alloc only.
Source§

impl<T> EncodeValue for EncodeValueRef<'_, T>
where T: EncodeValue,

Source§

impl<T, const N: usize> EncodeValue for SequenceOf<T, N>
where T: Encode,

Available on crate feature heapless only.
Source§

impl<T, const N: usize> EncodeValue for SetOf<T, N>
where T: Encode + DerOrd,

Available on (crate features alloc or heapless) and crate feature heapless only.
Source§

impl<const MAX_SIZE: usize> EncodeValue for ObjectIdentifier<MAX_SIZE>

Available on crate feature oid only.