Skip to content

Commit db210a5

Browse files
committed
der: add IntLike and UintLike traits
1 parent 5d46ec0 commit db210a5

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

der/src/asn1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub use self::{
4242
general_string::GeneralStringRef,
4343
generalized_time::GeneralizedTime,
4444
ia5_string::Ia5StringRef,
45-
integer::{int::IntRef, uint::UintRef},
45+
integer::{IntLike, UintLike, int::IntRef, uint::UintRef},
4646
null::Null,
4747
octet_string::OctetStringRef,
4848
printable_string::PrintableStringRef,

der/src/asn1/integer.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
pub(super) mod int;
44
pub(super) mod uint;
55

6+
#[cfg(doc)]
7+
use int::IntRef;
8+
#[cfg(doc)]
9+
use uint::UintRef;
10+
611
use core::{cmp::Ordering, mem::size_of};
712

813
use crate::{EncodeValue, Result, encode::encode_value_to_slice};
@@ -30,6 +35,22 @@ where
3035
Ok(buf1.cmp(buf2))
3136
}
3237

38+
/// Borrow the inner byte slice of [`IntRef`], or an owned `Int`
39+
pub trait IntLike {
40+
/// Borrow the inner byte slice which contains the least significant bytes
41+
/// of a big endian integer value with all leading ones stripped.
42+
#[must_use]
43+
fn as_int_bytes(&self) -> &[u8];
44+
}
45+
46+
/// Borrow the inner byte slice of [`UintRef`], or an owned `Uint`
47+
pub trait UintLike {
48+
/// Borrow the inner byte slice which contains the least significant bytes
49+
/// of a big endian integer value with all leading zeros stripped.
50+
#[must_use]
51+
fn as_uint_bytes(&self) -> &[u8];
52+
}
53+
3354
#[cfg(test)]
3455
#[allow(clippy::unwrap_used)]
3556
pub(crate) mod tests {

der/src/asn1/integer/int.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::{is_highest_bit_set, uint, value_cmp};
44
use crate::{
55
AnyRef, BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader,
6-
Result, Tag, ValueOrd, Writer, ord::OrdIsValueOrd,
6+
Result, Tag, ValueOrd, Writer, asn1::integer::IntLike, ord::OrdIsValueOrd,
77
};
88
use core::cmp::Ordering;
99

@@ -180,13 +180,19 @@ impl FixedTag for IntRef<'_> {
180180

181181
impl OrdIsValueOrd for IntRef<'_> {}
182182

183+
impl IntLike for IntRef<'_> {
184+
fn as_int_bytes(&self) -> &[u8] {
185+
self.inner.as_slice()
186+
}
187+
}
188+
183189
#[cfg(feature = "alloc")]
184190
mod allocating {
185191
use super::{IntRef, strip_leading_ones, validate_canonical};
186192
use crate::{
187193
BytesOwned, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader,
188194
Result, Tag, Writer,
189-
asn1::Uint,
195+
asn1::{Uint, integer::IntLike},
190196
ord::OrdIsValueOrd,
191197
referenced::{OwnedToRef, RefToOwned},
192198
};
@@ -237,6 +243,12 @@ mod allocating {
237243
}
238244
}
239245

246+
impl IntLike for Int {
247+
fn as_int_bytes(&self) -> &[u8] {
248+
self.inner.as_slice()
249+
}
250+
}
251+
240252
impl_any_conversions!(Int);
241253

242254
impl<'a> DecodeValue<'a> for Int {

der/src/asn1/integer/uint.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use super::value_cmp;
44
use crate::{
55
AnyRef, BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader,
6-
Result, Tag, ValueOrd, Writer, ord::OrdIsValueOrd, referenced::OwnedToRef,
6+
Result, Tag, ValueOrd, Writer, asn1::integer::UintLike, ord::OrdIsValueOrd,
7+
referenced::OwnedToRef,
78
};
89
use core::cmp::Ordering;
910

@@ -179,12 +180,19 @@ impl<'a> OwnedToRef for UintRef<'a> {
179180
}
180181
}
181182

183+
impl<'a> UintLike for UintRef<'a> {
184+
fn as_uint_bytes(&self) -> &[u8] {
185+
self.inner.as_slice()
186+
}
187+
}
188+
182189
#[cfg(feature = "alloc")]
183190
mod allocating {
184191
use super::{UintRef, decode_to_slice, encoded_len, strip_leading_zeroes};
185192
use crate::{
186193
BytesOwned, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader,
187194
Result, Tag, Writer,
195+
asn1::integer::UintLike,
188196
ord::OrdIsValueOrd,
189197
referenced::{OwnedToRef, RefToOwned},
190198
};
@@ -300,6 +308,12 @@ mod allocating {
300308
}
301309
}
302310

311+
impl UintLike for Uint {
312+
fn as_uint_bytes(&self) -> &[u8] {
313+
self.inner.as_slice()
314+
}
315+
}
316+
303317
macro_rules! impl_from_traits {
304318
($($uint:ty),+) => {
305319
$(

0 commit comments

Comments
 (0)