Skip to content

Commit 6ffe269

Browse files
authored
Remove now unneeded aliases (#500)
1 parent 8573264 commit 6ffe269

File tree

5 files changed

+45
-98
lines changed

5 files changed

+45
-98
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ asn1 = { version = "0.19", default-features = false }
2424

2525
## Changelog
2626

27+
### [0.20.0]
28+
29+
#### :rotating_light: Breaking changes
30+
31+
- Removed `Writer::{write_explicit_element, write_optional_explicit_element, write_implicit_element, write_optional_implicit_element}`.
32+
These can all be better accomplished with the `asn1::Explicit` and
33+
`asn1::Implicit` types.
34+
2735
### [0.19.0]
2836

2937
#### :rotating_light: Breaking changes

asn1_derive/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,23 +626,27 @@ fn generate_write_element(
626626
let value = arg.value;
627627
if arg.required {
628628
quote::quote_spanned! {f.span() =>
629-
w.write_explicit_element(#field_read, #value)?;
629+
w.write_element(&asn1::Explicit::<_, #value>::new(#field_read))?;
630630
}
631631
} else {
632632
quote::quote_spanned! {f.span() =>
633-
w.write_optional_explicit_element(#field_read, #value)?;
633+
if let Some(v) = #field_read {
634+
w.write_element(&asn1::Explicit::<_, #value>::new(v))?;
635+
}
634636
}
635637
}
636638
}
637639
OpType::Implicit(arg) => {
638640
let value = arg.value;
639641
if arg.required {
640642
quote::quote_spanned! {f.span() =>
641-
w.write_implicit_element(#field_read, #value)?;
643+
w.write_element(&asn1::Implicit::<_, #value>::new(#field_read))?;
642644
}
643645
} else {
644646
quote::quote_spanned! {f.span() =>
645-
w.write_optional_implicit_element(#field_read, #value)?;
647+
if let Some(v) = #field_read {
648+
w.write_element(&asn1::Implicit::<_, #value>::new(v))?;
649+
}
646650
}
647651
}
648652
}
@@ -731,13 +735,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma
731735
OpType::Explicit(arg) => {
732736
let tag = arg.value;
733737
quote::quote! {
734-
#name::#ident(value) => w.write_explicit_element(&value, #tag),
738+
#name::#ident(value) => w.write_element(&asn1::Explicit::<_, #tag>::new(value)),
735739
}
736740
}
737741
OpType::Implicit(arg) => {
738742
let tag = arg.value;
739743
quote::quote! {
740-
#name::#ident(value) => w.write_implicit_element(&value, #tag),
744+
#name::#ident(value) => w.write_element(&asn1::Implicit::<_, #tag>::new(value)),
741745
}
742746
}
743747
OpType::DefinedBy(_) => panic!("Can't use #[defined_by] in an Asn1Write on an enum"),

src/types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,13 @@ impl<T: Asn1Writable, const TAG: u32> SimpleAsn1Writable for Explicit<T, { TAG }
18091809
}
18101810
}
18111811

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+
18121819
impl<'a, T: Asn1Readable<'a>, U: Asn1DefinedByReadable<'a, T>, const TAG: u32>
18131820
Asn1DefinedByReadable<'a, T> for Explicit<U, { TAG }>
18141821
{

src/writer.rs

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::types::{Asn1Writable, SimpleAsn1Writable};
1+
use crate::types::Asn1Writable;
22
use crate::Tag;
33
#[cfg(not(feature = "std"))]
44
use alloc::vec::Vec;
@@ -107,50 +107,6 @@ impl Writer<'_> {
107107
val.write(self)
108108
}
109109

110-
/// This is an alias for `write_element::<Explicit<T, tag>>`.
111-
pub fn write_explicit_element<T: Asn1Writable>(&mut self, val: &T, tag: u32) -> WriteResult {
112-
let tag = crate::explicit_tag(tag);
113-
self.write_tlv(tag, |dest| Writer::new(dest).write_element(val))
114-
}
115-
116-
/// This is an alias for `write_element::<Option<Explicit<T, tag>>>`.
117-
pub fn write_optional_explicit_element<T: Asn1Writable>(
118-
&mut self,
119-
val: &Option<T>,
120-
tag: u32,
121-
) -> WriteResult {
122-
if let Some(v) = val {
123-
let tag = crate::explicit_tag(tag);
124-
self.write_tlv(tag, |dest| Writer::new(dest).write_element(v))
125-
} else {
126-
Ok(())
127-
}
128-
}
129-
130-
/// This is an alias for `write_element::<Implicit<T, tag>>`.
131-
pub fn write_implicit_element<T: SimpleAsn1Writable>(
132-
&mut self,
133-
val: &T,
134-
tag: u32,
135-
) -> WriteResult {
136-
let tag = crate::implicit_tag(tag, T::TAG);
137-
self.write_tlv(tag, |dest| val.write_data(dest))
138-
}
139-
140-
/// This is an alias for `write_element::<Option<Implicit<T, tag>>>`.
141-
pub fn write_optional_implicit_element<T: SimpleAsn1Writable>(
142-
&mut self,
143-
val: &Option<T>,
144-
tag: u32,
145-
) -> WriteResult {
146-
if let Some(v) = val {
147-
let tag = crate::implicit_tag(tag, T::TAG);
148-
self.write_tlv(tag, |dest| v.write_data(dest))
149-
} else {
150-
Ok(())
151-
}
152-
}
153-
154110
/// Writes a TLV with the specified tag where the value is any bytes
155111
/// written to the `Vec` in the callback. The length portion of the
156112
/// TLV is automatically computed.
@@ -716,39 +672,6 @@ mod tests {
716672
(Implicit::new(true), b"\x82\x01\xff"),
717673
(Implicit::new(false), b"\x82\x01\x00"),
718674
]);
719-
720-
assert_eq!(
721-
write(|w| { w.write_optional_implicit_element(&Some(true), 2) }).unwrap(),
722-
b"\x82\x01\xff"
723-
);
724-
assert_eq!(
725-
write(|w| { w.write_optional_explicit_element::<u8>(&None, 2) }).unwrap(),
726-
b""
727-
);
728-
729-
assert_eq!(
730-
write(|w| {
731-
w.write_optional_implicit_element(&Some(SequenceWriter::new(&|_w| Ok(()))), 2)
732-
})
733-
.unwrap(),
734-
b"\xa2\x00"
735-
);
736-
assert_eq!(
737-
write(|w| { w.write_optional_explicit_element::<SequenceWriter<'_>>(&None, 2) })
738-
.unwrap(),
739-
b""
740-
);
741-
742-
assert_eq!(
743-
write(|w| { w.write_implicit_element(&true, 2) }).unwrap(),
744-
b"\x82\x01\xff"
745-
);
746-
747-
assert_eq!(
748-
write(|w| { w.write_implicit_element(&SequenceWriter::new(&|_w| { Ok(()) }), 2) })
749-
.unwrap(),
750-
b"\xa2\x00"
751-
);
752675
}
753676

754677
#[test]
@@ -757,20 +680,6 @@ mod tests {
757680
(Explicit::new(true), b"\xa2\x03\x01\x01\xff"),
758681
(Explicit::new(false), b"\xa2\x03\x01\x01\x00"),
759682
]);
760-
761-
assert_eq!(
762-
write(|w| { w.write_optional_explicit_element(&Some(true), 2) }).unwrap(),
763-
b"\xa2\x03\x01\x01\xff"
764-
);
765-
assert_eq!(
766-
write(|w| { w.write_optional_explicit_element::<u8>(&None, 2) }).unwrap(),
767-
b""
768-
);
769-
770-
assert_eq!(
771-
write(|w| { w.write_explicit_element(&true, 2) }).unwrap(),
772-
b"\xa2\x03\x01\x01\xff"
773-
);
774683
}
775684

776685
#[test]

tests/derive_test.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ fn test_explicit() {
117117
]);
118118
}
119119

120+
#[test]
121+
fn test_explicit_tlv() {
122+
#[derive(asn1::Asn1Read, asn1::Asn1Write, Debug, PartialEq, Eq)]
123+
struct ExplicitTlv<'a> {
124+
#[explicit(5)]
125+
a: Option<asn1::Tlv<'a>>,
126+
}
127+
128+
assert_roundtrips(&[
129+
(Ok(ExplicitTlv { a: None }), b"\x30\x00"),
130+
(
131+
Ok(ExplicitTlv {
132+
a: asn1::parse_single(b"\x05\x00").unwrap(),
133+
}),
134+
b"\x30\x04\xa5\x02\x05\x00",
135+
),
136+
]);
137+
}
138+
120139
#[test]
121140
fn test_implicit() {
122141
#[derive(asn1::Asn1Read, asn1::Asn1Write, Debug, PartialEq, Eq)]

0 commit comments

Comments
 (0)