@@ -41,8 +41,8 @@ use crate::{FixedTag, Tag};
4141/// self.0.encoded_len()
4242/// }
4343///
44- /// fn encode(&self, encoder : &mut impl Writer) -> der::Result<()> {
45- /// self.0.encode(encoder )
44+ /// fn encode(&self, writer : &mut impl Writer) -> der::Result<()> {
45+ /// self.0.encode(writer )
4646/// }
4747/// }
4848/// # }
@@ -55,7 +55,7 @@ pub trait Encode {
5555 fn encoded_len ( & self ) -> Result < Length > ;
5656
5757 /// Encode this TLV object as ASN.1 DER using the provided [`Writer`].
58- fn encode ( & self , encoder : & mut impl Writer ) -> Result < ( ) > ;
58+ fn encode ( & self , writer : & mut impl Writer ) -> Result < ( ) > ;
5959
6060 /// Encode this TLV object to the provided byte slice, returning a sub-slice
6161 /// containing the encoded message.
@@ -167,6 +167,36 @@ where
167167///
168168/// When [`EncodeValue`] is paired with [`FixedTag`],
169169/// it produces a complete TLV ASN.1 DER encoding as [`Encode`] trait.
170+ ///
171+ /// ## Example
172+ /// ```
173+ /// use der::{Encode, EncodeValue, ErrorKind, FixedTag, Length, Tag, Writer};
174+ ///
175+ /// /// 1-byte month
176+ /// struct MyByteMonth(u8);
177+ ///
178+ /// impl EncodeValue for MyByteMonth {
179+ ///
180+ /// fn value_len(&self) -> der::Result<Length> {
181+ /// Ok(Length::new(1))
182+ /// }
183+ ///
184+ /// fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
185+ /// writer.write_byte(self.0)?;
186+ /// Ok(())
187+ /// }
188+ /// }
189+ ///
190+ /// impl FixedTag for MyByteMonth {
191+ /// const TAG: Tag = Tag::OctetString;
192+ /// }
193+ ///
194+ /// let month = MyByteMonth(9);
195+ /// let mut buf = [0u8; 16];
196+ /// let month_der = month.encode_to_slice(&mut buf).expect("month to encode");
197+ ///
198+ /// assert_eq!(month_der, b"\x04\x01\x09");
199+ /// ```
170200pub trait EncodeValue {
171201 /// Get the [`Header`] used to encode this value.
172202 fn header ( & self ) -> Result < Header >
@@ -182,7 +212,7 @@ pub trait EncodeValue {
182212
183213 /// Encode value (sans [`Tag`]+[`Length`] header) as ASN.1 DER using the
184214 /// provided [`Writer`].
185- fn encode_value ( & self , encoder : & mut impl Writer ) -> Result < ( ) > ;
215+ fn encode_value ( & self , writer : & mut impl Writer ) -> Result < ( ) > ;
186216}
187217
188218#[ cfg( feature = "alloc" ) ]
@@ -203,7 +233,7 @@ pub(crate) fn encode_value_to_slice<'a, T>(buf: &'a mut [u8], value: &T) -> Resu
203233where
204234 T : EncodeValue ,
205235{
206- let mut encoder = SliceWriter :: new ( buf) ;
207- value. encode_value ( & mut encoder ) ?;
208- encoder . finish ( )
236+ let mut writer = SliceWriter :: new ( buf) ;
237+ value. encode_value ( & mut writer ) ?;
238+ writer . finish ( )
209239}
0 commit comments