@@ -19,24 +19,21 @@ use {
1919///
2020/// This trait describes how to encode a given type.
2121pub trait Encode {
22- /// Type returned in the event of an encoding error.
23- type Error : From < Error > ;
24-
2522 /// Get the length of this type encoded in bytes, prior to Base64 encoding.
26- fn encoded_len ( & self ) -> Result < usize , Self :: Error > ;
23+ fn encoded_len ( & self ) -> Result < usize , Error > ;
2724
2825 /// Encode this value using the provided [`Writer`].
29- fn encode ( & self , writer : & mut impl Writer ) -> Result < ( ) , Self :: Error > ;
26+ fn encode ( & self , writer : & mut impl Writer ) -> Result < ( ) , Error > ;
3027
3128 /// Return the length of this type after encoding when prepended with a
3229 /// `uint32` length prefix.
33- fn encoded_len_prefixed ( & self ) -> Result < usize , Self :: Error > {
34- Ok ( [ 4 , self . encoded_len ( ) ?] . checked_sum ( ) ? )
30+ fn encoded_len_prefixed ( & self ) -> Result < usize , Error > {
31+ [ 4 , self . encoded_len ( ) ?] . checked_sum ( )
3532 }
3633
3734 /// Encode this value, first prepending a `uint32` length prefix
3835 /// set to [`Encode::encoded_len`].
39- fn encode_prefixed ( & self , writer : & mut impl Writer ) -> Result < ( ) , Self :: Error > {
36+ fn encode_prefixed ( & self , writer : & mut impl Writer ) -> Result < ( ) , Error > {
4037 self . encoded_len ( ) ?. encode ( writer) ?;
4138 self . encode ( writer)
4239 }
@@ -50,36 +47,28 @@ pub trait Encode {
5047pub trait EncodePem : Encode + PemLabel {
5148 /// Encode this type using the [`Encode`] trait, writing the resulting PEM
5249 /// document into the provided `out` buffer.
53- fn encode_pem < ' o > (
54- & self ,
55- line_ending : LineEnding ,
56- out : & ' o mut [ u8 ] ,
57- ) -> Result < & ' o str , Self :: Error > ;
50+ fn encode_pem < ' o > ( & self , line_ending : LineEnding , out : & ' o mut [ u8 ] ) -> Result < & ' o str , Error > ;
5851
5952 /// Encode this type using the [`Encode`] trait, writing the resulting PEM
6053 /// document to a returned [`String`].
6154 #[ cfg( feature = "alloc" ) ]
62- fn encode_pem_string ( & self , line_ending : LineEnding ) -> Result < String , Self :: Error > ;
55+ fn encode_pem_string ( & self , line_ending : LineEnding ) -> Result < String , Error > ;
6356}
6457
6558#[ cfg( feature = "pem" ) ]
6659impl < T : Encode + PemLabel > EncodePem for T {
67- fn encode_pem < ' o > (
68- & self ,
69- line_ending : LineEnding ,
70- out : & ' o mut [ u8 ] ,
71- ) -> Result < & ' o str , Self :: Error > {
60+ fn encode_pem < ' o > ( & self , line_ending : LineEnding , out : & ' o mut [ u8 ] ) -> Result < & ' o str , Error > {
7261 let mut writer =
7362 pem:: Encoder :: new_wrapped ( Self :: PEM_LABEL , PEM_LINE_WIDTH , line_ending, out)
7463 . map_err ( Error :: from) ?;
7564
7665 self . encode ( & mut writer) ?;
7766 let encoded_len = writer. finish ( ) . map_err ( Error :: from) ?;
78- Ok ( str:: from_utf8 ( & out[ ..encoded_len] ) . map_err ( Error :: from) ? )
67+ str:: from_utf8 ( & out[ ..encoded_len] ) . map_err ( Error :: from)
7968 }
8069
8170 #[ cfg( feature = "alloc" ) ]
82- fn encode_pem_string ( & self , line_ending : LineEnding ) -> Result < String , Self :: Error > {
71+ fn encode_pem_string ( & self , line_ending : LineEnding ) -> Result < String , Error > {
8372 let encoded_len = pem:: encapsulated_len_wrapped (
8473 Self :: PEM_LABEL ,
8574 PEM_LINE_WIDTH ,
@@ -91,14 +80,12 @@ impl<T: Encode + PemLabel> EncodePem for T {
9180 let mut buf = vec ! [ 0u8 ; encoded_len] ;
9281 let actual_len = self . encode_pem ( line_ending, & mut buf) ?. len ( ) ;
9382 buf. truncate ( actual_len) ;
94- Ok ( String :: from_utf8 ( buf) . map_err ( Error :: from) ? )
83+ String :: from_utf8 ( buf) . map_err ( Error :: from)
9584 }
9685}
9786
9887/// Encode a single `byte` to the writer.
9988impl Encode for u8 {
100- type Error = Error ;
101-
10289 fn encoded_len ( & self ) -> Result < usize , Error > {
10390 Ok ( 1 )
10491 }
@@ -116,8 +103,6 @@ impl Encode for u8 {
116103///
117104/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
118105impl Encode for u32 {
119- type Error = Error ;
120-
121106 fn encoded_len ( & self ) -> Result < usize , Error > {
122107 Ok ( 4 )
123108 }
@@ -134,8 +119,6 @@ impl Encode for u32 {
134119///
135120/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
136121impl Encode for u64 {
137- type Error = Error ;
138-
139122 fn encoded_len ( & self ) -> Result < usize , Error > {
140123 Ok ( 8 )
141124 }
@@ -152,8 +135,6 @@ impl Encode for u64 {
152135///
153136/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
154137impl Encode for usize {
155- type Error = Error ;
156-
157138 fn encoded_len ( & self ) -> Result < usize , Error > {
158139 Ok ( 4 )
159140 }
@@ -171,8 +152,6 @@ impl Encode for usize {
171152///
172153/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
173154impl Encode for [ u8 ] {
174- type Error = Error ;
175-
176155 fn encoded_len ( & self ) -> Result < usize , Error > {
177156 [ 4 , self . len ( ) ] . checked_sum ( )
178157 }
@@ -191,8 +170,6 @@ impl Encode for [u8] {
191170///
192171/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
193172impl < const N : usize > Encode for [ u8 ; N ] {
194- type Error = Error ;
195-
196173 fn encoded_len ( & self ) -> Result < usize , Error > {
197174 self . as_slice ( ) . encoded_len ( )
198175 }
@@ -220,8 +197,6 @@ impl<const N: usize> Encode for [u8; N] {
220197///
221198/// [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5
222199impl Encode for & str {
223- type Error = Error ;
224-
225200 fn encoded_len ( & self ) -> Result < usize , Error > {
226201 self . as_bytes ( ) . encoded_len ( )
227202 }
@@ -233,8 +208,6 @@ impl Encode for &str {
233208
234209#[ cfg( feature = "alloc" ) ]
235210impl Encode for Vec < u8 > {
236- type Error = Error ;
237-
238211 fn encoded_len ( & self ) -> Result < usize , Error > {
239212 self . as_slice ( ) . encoded_len ( )
240213 }
@@ -246,8 +219,6 @@ impl Encode for Vec<u8> {
246219
247220#[ cfg( feature = "alloc" ) ]
248221impl Encode for String {
249- type Error = Error ;
250-
251222 fn encoded_len ( & self ) -> Result < usize , Error > {
252223 self . as_str ( ) . encoded_len ( )
253224 }
@@ -259,8 +230,6 @@ impl Encode for String {
259230
260231#[ cfg( feature = "alloc" ) ]
261232impl Encode for Vec < String > {
262- type Error = Error ;
263-
264233 fn encoded_len ( & self ) -> Result < usize , Error > {
265234 self . iter ( ) . try_fold ( 4usize , |acc, string| {
266235 acc. checked_add ( string. encoded_len ( ) ?) . ok_or ( Error :: Length )
0 commit comments