Skip to content

Conversation

@dishmaker
Copy link
Contributor

@dishmaker dishmaker commented Oct 21, 2025

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");

@dishmaker dishmaker changed the title der: docs: add EncodeValue trait example + doc fn decode_value der: docs: add EncodeValue trait example Oct 27, 2025
@baloo baloo merged commit d4c7f25 into RustCrypto:master Oct 27, 2025
107 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants