@@ -17,28 +17,30 @@ use {
1717use crate :: ErrorKind ;
1818
1919#[ cfg( doc) ]
20- use crate :: Tag ;
20+ use crate :: { FixedTag , Tag } ;
2121
22- /// Encoding trait.
22+ /// Encode trait produces a complete TLV (Tag-Length-Value) structure.
23+ ///
24+ /// As opposed to [`EncodeValue`], implementer is expected to write ASN.1 DER tag and length header before value.
2325#[ diagnostic:: on_unimplemented(
2426 note = "Consider adding impls of `EncodeValue` and `FixedTag` to `{Self}`"
2527) ]
2628pub trait Encode {
27- /// Compute the length of this value in bytes when encoded as ASN.1 DER.
29+ /// Compute the length of this TLV object in bytes when encoded as ASN.1 DER.
2830 fn encoded_len ( & self ) -> Result < Length > ;
2931
30- /// Encode this value as ASN.1 DER using the provided [`Writer`].
32+ /// Encode this TLV object as ASN.1 DER using the provided [`Writer`].
3133 fn encode ( & self , encoder : & mut impl Writer ) -> Result < ( ) > ;
3234
33- /// Encode this value to the provided byte slice, returning a sub-slice
35+ /// Encode this TLV object to the provided byte slice, returning a sub-slice
3436 /// containing the encoded message.
3537 fn encode_to_slice < ' a > ( & self , buf : & ' a mut [ u8 ] ) -> Result < & ' a [ u8 ] > {
3638 let mut writer = SliceWriter :: new ( buf) ;
3739 self . encode ( & mut writer) ?;
3840 writer. finish ( )
3941 }
4042
41- /// Encode this message as ASN.1 DER, appending it to the provided
43+ /// Encode this TLV object as ASN.1 DER, appending it to the provided
4244 /// byte vector.
4345 #[ cfg( feature = "alloc" ) ]
4446 fn encode_to_vec ( & self , buf : & mut Vec < u8 > ) -> Result < Length > {
@@ -60,7 +62,7 @@ pub trait Encode {
6062 actual_len. try_into ( )
6163 }
6264
63- /// Encode this type as DER, returning a byte vector.
65+ /// Encode this TLV object as ASN.1 DER, returning a byte vector.
6466 #[ cfg( feature = "alloc" ) ]
6567 fn to_der ( & self ) -> Result < Vec < u8 > > {
6668 let mut buf = Vec :: new ( ) ;
@@ -73,12 +75,12 @@ impl<T> Encode for T
7375where
7476 T : EncodeValue + Tagged + ?Sized ,
7577{
76- /// Compute the length of this value in bytes when encoded as ASN.1 DER.
78+ /// Compute the length of this TLV object in bytes when encoded as ASN.1 DER.
7779 fn encoded_len ( & self ) -> Result < Length > {
7880 self . value_len ( ) . and_then ( |len| len. for_tlv ( self . tag ( ) ) )
7981 }
8082
81- /// Encode this value as ASN.1 DER using the provided [`Writer`].
83+ /// Encode this TLV object as ASN.1 DER using the provided [`Writer`].
8284 fn encode ( & self , writer : & mut impl Writer ) -> Result < ( ) > {
8385 self . header ( ) ?. encode ( writer) ?;
8486 self . encode_value ( writer)
@@ -134,6 +136,12 @@ where
134136
135137/// Encode the value part of a Tag-Length-Value encoded field, sans the [`Tag`]
136138/// and [`Length`].
139+ ///
140+ /// As opposed to [`Encode`], implementer is expected to write the inner content only,
141+ /// without the [`Header`].
142+ ///
143+ /// When [`EncodeValue`] is paired with [`FixedTag`],
144+ /// it produces a complete TLV ASN.1 DER encoding as [`Encode`] trait.
137145pub trait EncodeValue {
138146 /// Get the [`Header`] used to encode this value.
139147 fn header ( & self ) -> Result < Header >
0 commit comments