Skip to content

Commit 96c42b2

Browse files
authored
Fixed taking an explicit reference to an enum (#504)
1 parent 8ad493d commit 96c42b2

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

asn1_derive/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ pub fn derive_asn1_write(input: proc_macro::TokenStream) -> proc_macro::TokenStr
9090
#write_block
9191
}
9292
}
93+
94+
impl #impl_generics asn1::Asn1Writable for &#name #ty_generics #where_clause {
95+
fn write(&self, w: &mut asn1::Writer) -> asn1::WriteResult {
96+
#name::write(self, w)
97+
}
98+
}
9399
}
94100
}
95101
_ => unimplemented!("Not supported for unions"),

src/types.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ impl Asn1Writable for Tlv<'_> {
145145
}
146146
}
147147

148+
impl Asn1Writable for &Tlv<'_> {
149+
#[inline]
150+
fn write(&self, w: &mut Writer<'_>) -> WriteResult {
151+
Tlv::write(self, w)
152+
}
153+
}
154+
148155
/// The ASN.1 NULL type, for use with `Parser.read_element` and
149156
/// `Writer.write_element`.
150157
pub type Null = ();
@@ -1809,13 +1816,6 @@ impl<T: Asn1Writable, const TAG: u32> SimpleAsn1Writable for Explicit<T, { TAG }
18091816
}
18101817
}
18111818

1812-
impl<const TAG: u32> SimpleAsn1Writable for Explicit<&'_ Tlv<'_>, { TAG }> {
1813-
const TAG: Tag = crate::explicit_tag(TAG);
1814-
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
1815-
self.inner.write(&mut Writer::new(dest))
1816-
}
1817-
}
1818-
18191819
impl<'a, T: Asn1Readable<'a>, U: Asn1DefinedByReadable<'a, T>, const TAG: u32>
18201820
Asn1DefinedByReadable<'a, T> for Explicit<U, { TAG }>
18211821
{

tests/derive_test.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,30 @@ fn test_enum_implicit() {
441441
]);
442442
}
443443

444+
#[test]
445+
fn test_enum_in_explicit() {
446+
#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Debug, Eq)]
447+
enum BasicChoice {
448+
A(u64),
449+
}
450+
451+
#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Debug, Eq)]
452+
struct StructWithExplicitChoice {
453+
#[explicit(0)]
454+
c: Option<BasicChoice>,
455+
}
456+
457+
assert_roundtrips(&[
458+
(Ok(StructWithExplicitChoice { c: None }), b"\x30\x00"),
459+
(
460+
Ok(StructWithExplicitChoice {
461+
c: Some(BasicChoice::A(3)),
462+
}),
463+
b"\x30\x05\xa0\x03\x02\x01\x03",
464+
),
465+
]);
466+
}
467+
444468
#[test]
445469
fn test_error_parse_location() {
446470
#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Debug, Eq)]

0 commit comments

Comments
 (0)