Skip to content

Commit e1890c3

Browse files
authored
make BigInt::new, BigUint::new, and DateTime::new const fns (#539)
* make BigInt::new, BigUint::new, and DateTime::new const fns * changelog * bump for 0.21.2 release
1 parent 2e1a8c4 commit e1890c3

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "asn1"
3-
version = "0.21.1"
3+
version = "0.21.2"
44
authors = ["Alex Gaynor <[email protected]>"]
55
repository = "https://github.com/alex/rust-asn1"
66
keywords = ["asn1"]
@@ -17,7 +17,7 @@ default = ["std"]
1717
std = []
1818

1919
[dependencies]
20-
asn1_derive = { path = "asn1_derive/", version = "0.21.1" }
20+
asn1_derive = { path = "asn1_derive/", version = "0.21.2" }
2121
itoa = "1.0.11"
2222

2323
[dev-dependencies]

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ $ cargo add asn1 --no-default-features
2323

2424
## Changelog
2525

26+
### [0.21.2]
27+
28+
#### Added
29+
30+
- `BigInt::new`, `BigUint::new`, and `DateTime::new` are now `const fn`.
31+
32+
2633
### [0.21.1]
2734

2835
#### Added

asn1_derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "asn1_derive"
3-
version = "0.21.1"
3+
version = "0.21.2"
44
authors = ["Alex Gaynor <[email protected]>"]
55
repository = "https://github.com/alex/rust-asn1"
66
license = "BSD-3-Clause"

src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub struct ParseError {
6262
}
6363

6464
impl ParseError {
65-
pub fn new(kind: ParseErrorKind) -> ParseError {
65+
pub const fn new(kind: ParseErrorKind) -> ParseError {
6666
ParseError {
6767
kind,
6868
parse_locations: [None, None, None, None],

src/types.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ impl SimpleAsn1Writable for UniversalString<'_> {
568568
}
569569
}
570570

571-
fn validate_integer(data: &[u8], signed: bool) -> ParseResult<()> {
571+
const fn validate_integer(data: &[u8], signed: bool) -> ParseResult<()> {
572572
if data.is_empty() {
573573
return Err(ParseError::new(ParseErrorKind::InvalidValue));
574574
}
@@ -656,9 +656,11 @@ impl<'a> BigUint<'a> {
656656
/// as required by DER: minimally and if the high bit would be set in the
657657
/// first octet, a leading `\x00` should be prepended (to disambiguate from
658658
/// negative values).
659-
pub fn new(data: &'a [u8]) -> Option<Self> {
660-
validate_integer(data, false).ok()?;
661-
Some(BigUint { data })
659+
pub const fn new(data: &'a [u8]) -> Option<Self> {
660+
match validate_integer(data, false) {
661+
Ok(()) => Some(BigUint { data }),
662+
Err(_) => None,
663+
}
662664
}
663665

664666
/// Returns the contents of the integer as big-endian bytes.
@@ -731,9 +733,11 @@ impl<'a> BigInt<'a> {
731733
/// as required by DER: minimally and if the high bit would be set in the
732734
/// first octet, a leading `\x00` should be prepended (to disambiguate from
733735
/// negative values).
734-
pub fn new(data: &'a [u8]) -> Option<Self> {
735-
validate_integer(data, true).ok()?;
736-
Some(BigInt { data })
736+
pub const fn new(data: &'a [u8]) -> Option<Self> {
737+
match validate_integer(data, true) {
738+
Ok(()) => Some(BigInt { data }),
739+
Err(_) => None,
740+
}
737741
}
738742

739743
/// Returns the contents of the integer as big-endian bytes.
@@ -874,7 +878,7 @@ fn read_4_digits(data: &mut &[u8]) -> ParseResult<u16> {
874878
+ u16::from(read_digit(data)?))
875879
}
876880

877-
fn validate_date(year: u16, month: u8, day: u8) -> ParseResult<()> {
881+
const fn validate_date(year: u16, month: u8, day: u8) -> ParseResult<()> {
878882
if day < 1 {
879883
return Err(ParseError::new(ParseErrorKind::InvalidValue));
880884
}
@@ -934,26 +938,28 @@ pub struct DateTime {
934938
}
935939

936940
impl DateTime {
937-
pub fn new(
941+
pub const fn new(
938942
year: u16,
939943
month: u8,
940944
day: u8,
941945
hour: u8,
942946
minute: u8,
943947
second: u8,
944948
) -> ParseResult<DateTime> {
945-
validate_date(year, month, day)?;
946949
if hour > 23 || minute > 59 || second > 59 {
947950
return Err(ParseError::new(ParseErrorKind::InvalidValue));
948951
}
949-
Ok(DateTime {
950-
year,
951-
month,
952-
day,
953-
hour,
954-
minute,
955-
second,
956-
})
952+
match validate_date(year, month, day) {
953+
Ok(()) => Ok(DateTime {
954+
year,
955+
month,
956+
day,
957+
hour,
958+
minute,
959+
second,
960+
}),
961+
Err(e) => Err(e),
962+
}
957963
}
958964

959965
/// The calendar year.

0 commit comments

Comments
 (0)