Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed taking an explicit reference to an enum #504

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions asn1_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ pub fn derive_asn1_write(input: proc_macro::TokenStream) -> proc_macro::TokenStr
#write_block
}
}

impl #impl_generics asn1::Asn1Writable for &#name #ty_generics #where_clause {
fn write(&self, w: &mut asn1::Writer) -> asn1::WriteResult {
#name::write(self, w)
}
}
}
}
_ => unimplemented!("Not supported for unions"),
Expand Down
14 changes: 7 additions & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ impl Asn1Writable for Tlv<'_> {
}
}

impl Asn1Writable for &Tlv<'_> {
#[inline]
fn write(&self, w: &mut Writer<'_>) -> WriteResult {
Tlv::write(self, w)
}
}

/// The ASN.1 NULL type, for use with `Parser.read_element` and
/// `Writer.write_element`.
pub type Null = ();
Expand Down Expand Up @@ -1809,13 +1816,6 @@ impl<T: Asn1Writable, const TAG: u32> SimpleAsn1Writable for Explicit<T, { TAG }
}
}

impl<const TAG: u32> SimpleAsn1Writable for Explicit<&'_ Tlv<'_>, { TAG }> {
const TAG: Tag = crate::explicit_tag(TAG);
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
self.inner.write(&mut Writer::new(dest))
}
}

impl<'a, T: Asn1Readable<'a>, U: Asn1DefinedByReadable<'a, T>, const TAG: u32>
Asn1DefinedByReadable<'a, T> for Explicit<U, { TAG }>
{
Expand Down
24 changes: 24 additions & 0 deletions tests/derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,30 @@ fn test_enum_implicit() {
]);
}

#[test]
fn test_enum_in_explicit() {
#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Debug, Eq)]
enum BasicChoice {
A(u64),
}

#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Debug, Eq)]
struct StructWithExplicitChoice {
#[explicit(0)]
c: Option<BasicChoice>,
}

assert_roundtrips(&[
(Ok(StructWithExplicitChoice { c: None }), b"\x30\x00"),
(
Ok(StructWithExplicitChoice {
c: Some(BasicChoice::A(3)),
}),
b"\x30\x05\xa0\x03\x02\x01\x03",
),
]);
}

#[test]
fn test_error_parse_location() {
#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Debug, Eq)]
Expand Down