File tree Expand file tree Collapse file tree 4 files changed +35
-16
lines changed Expand file tree Collapse file tree 4 files changed +35
-16
lines changed Original file line number Diff line number Diff line change @@ -40,9 +40,11 @@ impl<'a> AnyRef<'a> {
40
40
} ;
41
41
42
42
/// Create a new [`AnyRef`] from the provided [`Tag`] and DER bytes.
43
- pub fn new ( tag : Tag , bytes : & ' a [ u8 ] ) -> Result < Self , Error > {
44
- let value = BytesRef :: new ( bytes) . map_err ( |_| ErrorKind :: Length { tag } ) ?;
45
- Ok ( Self { tag, value } )
43
+ pub const fn new ( tag : Tag , bytes : & ' a [ u8 ] ) -> Result < Self , Error > {
44
+ match BytesRef :: new ( bytes) {
45
+ Ok ( value) => Ok ( Self { tag, value } ) ,
46
+ Err ( _) => Err ( Error :: from_kind ( ErrorKind :: Length { tag } ) ) ,
47
+ }
46
48
}
47
49
48
50
/// Infallible creation of an [`AnyRef`] from a [`BytesRef`].
Original file line number Diff line number Diff line change @@ -28,11 +28,14 @@ impl<'a> BytesRef<'a> {
28
28
29
29
/// Create a new [`BytesRef`], ensuring that the provided `slice` value
30
30
/// is shorter than `Length::max()`.
31
- pub fn new ( slice : & ' a [ u8 ] ) -> Result < Self > {
32
- Ok ( Self {
33
- length : Length :: try_from ( slice. len ( ) ) ?,
34
- inner : slice,
35
- } )
31
+ pub const fn new ( slice : & ' a [ u8 ] ) -> Result < Self > {
32
+ match Length :: new_usize ( slice. len ( ) ) {
33
+ Ok ( length) => Ok ( Self {
34
+ length,
35
+ inner : slice,
36
+ } ) ,
37
+ Err ( err) => Err ( err) ,
38
+ }
36
39
}
37
40
38
41
/// Borrow the inner byte slice
Original file line number Diff line number Diff line change @@ -26,12 +26,19 @@ pub struct Error {
26
26
27
27
impl Error {
28
28
/// Create a new [`Error`].
29
- pub fn new ( kind : ErrorKind , position : Length ) -> Error {
29
+ pub const fn new ( kind : ErrorKind , position : Length ) -> Error {
30
30
Error {
31
31
kind,
32
32
position : Some ( position) ,
33
33
}
34
34
}
35
+ /// Create a new [`Error`], without known position.
36
+ pub ( crate ) const fn from_kind ( kind : ErrorKind ) -> Error {
37
+ Error {
38
+ kind,
39
+ position : None ,
40
+ }
41
+ }
35
42
36
43
/// Create a new [`ErrorKind::Incomplete`] for the given length.
37
44
///
@@ -86,10 +93,7 @@ impl fmt::Display for Error {
86
93
87
94
impl From < ErrorKind > for Error {
88
95
fn from ( kind : ErrorKind ) -> Error {
89
- Error {
90
- kind,
91
- position : None ,
92
- }
96
+ Error :: from_kind ( kind)
93
97
}
94
98
}
95
99
Original file line number Diff line number Diff line change @@ -44,6 +44,18 @@ impl Length {
44
44
Self ( value as u32 )
45
45
}
46
46
47
+ /// Create a new [`Length`] for any value which fits inside the length type.
48
+ ///
49
+ /// This function is const-safe and therefore useful for [`Length`] constants.
50
+ #[ allow( clippy:: cast_possible_truncation) ]
51
+ pub ( crate ) const fn new_usize ( len : usize ) -> Result < Self > {
52
+ if len > ( u32:: MAX as usize ) {
53
+ Err ( Error :: from_kind ( ErrorKind :: Overflow ) )
54
+ } else {
55
+ Ok ( Length ( len as u32 ) )
56
+ }
57
+ }
58
+
47
59
/// Is this length equal to zero?
48
60
pub fn is_zero ( self ) -> bool {
49
61
self == Self :: ZERO
@@ -192,9 +204,7 @@ impl TryFrom<usize> for Length {
192
204
type Error = Error ;
193
205
194
206
fn try_from ( len : usize ) -> Result < Length > {
195
- u32:: try_from ( len)
196
- . map_err ( |_| ErrorKind :: Overflow ) ?
197
- . try_into ( )
207
+ Length :: new_usize ( len)
198
208
}
199
209
}
200
210
You can’t perform that action at this time.
0 commit comments