Skip to content

Commit fa57915

Browse files
committed
Remove implementation of SimpleAsn1Writable for &T
I don't know if this is a good idea.
1 parent f8ca030 commit fa57915

File tree

3 files changed

+29
-48
lines changed

3 files changed

+29
-48
lines changed

asn1_derive/src/lib.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub fn derive_asn1_read(input: proc_macro::TokenStream) -> proc_macro::TokenStre
1717
all_field_types(&input.data, &input.generics),
1818
syn::parse_quote!(asn1::Asn1Readable<#lifetime_name>),
1919
syn::parse_quote!(asn1::Asn1DefinedByReadable<#lifetime_name, asn1::ObjectIdentifier>),
20-
false,
2120
);
2221
let (impl_generics, _, where_clause) = generics.split_for_impl();
2322

@@ -67,7 +66,6 @@ pub fn derive_asn1_write(input: proc_macro::TokenStream) -> proc_macro::TokenStr
6766
fields,
6867
syn::parse_quote!(asn1::Asn1Writable),
6968
syn::parse_quote!(asn1::Asn1DefinedByWritable<asn1::ObjectIdentifier>),
70-
true,
7169
);
7270
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
7371

@@ -402,7 +400,6 @@ fn add_bounds(
402400
field_types: Vec<(syn::Type, OpType, bool)>,
403401
bound: syn::TypeParamBound,
404402
defined_by_bound: syn::TypeParamBound,
405-
add_ref: bool,
406403
) {
407404
let where_clause = if field_types.is_empty() {
408405
return;
@@ -416,11 +413,11 @@ fn add_bounds(
416413
};
417414

418415
for (f, op_type, has_default) in field_types {
419-
let (bounded_ty, required_bound) = match (op_type, add_ref) {
420-
(OpType::Regular, _) => (f, bound.clone()),
421-
(OpType::DefinedBy(_), _) => (f, defined_by_bound.clone()),
416+
let (bounded_ty, required_bound) = match op_type {
417+
OpType::Regular => (f, bound.clone()),
418+
OpType::DefinedBy(_) => (f, defined_by_bound.clone()),
422419

423-
(OpType::Implicit(OpTypeArgs { value, required }), false) => {
420+
OpType::Implicit(OpTypeArgs { value, required }) => {
424421
let ty = if required || has_default {
425422
syn::parse_quote!(asn1::Implicit::<#f, #value>)
426423
} else {
@@ -429,32 +426,14 @@ fn add_bounds(
429426

430427
(ty, bound.clone())
431428
}
432-
(OpType::Implicit(OpTypeArgs { value, required }), true) => {
433-
let ty = if required || has_default {
434-
syn::parse_quote!(for<'asn1_internal> asn1::Implicit::<&'asn1_internal #f, #value>)
435-
} else {
436-
syn::parse_quote!(for<'asn1_internal> asn1::Implicit::<&'asn1_internal <#f as asn1::OptionExt>::T, #value>)
437-
};
438-
439-
(ty, bound.clone())
440-
}
441429

442-
(OpType::Explicit(OpTypeArgs { value, required }), false) => {
430+
OpType::Explicit(OpTypeArgs { value, required }) => {
443431
let ty = if required || has_default {
444432
syn::parse_quote!(asn1::Explicit::<#f, #value>)
445433
} else {
446434
syn::parse_quote!(asn1::Explicit::<<#f as asn1::OptionExt>::T, #value>)
447435
};
448436

449-
(ty, bound.clone())
450-
}
451-
(OpType::Explicit(OpTypeArgs { value, required }), true) => {
452-
let ty = if required || has_default {
453-
syn::parse_quote!(for<'asn1_internal> asn1::Explicit::<&'asn1_internal #f, #value>)
454-
} else {
455-
syn::parse_quote!(for<'asn1_internal> asn1::Explicit::<&'asn1_internal <#f as asn1::OptionExt>::T, #value>)
456-
};
457-
458437
(ty, bound.clone())
459438
}
460439
};
@@ -750,8 +729,9 @@ fn generate_write_element(
750729
) -> proc_macro2::TokenStream {
751730
let (write_type, default) = extract_field_properties(&f.attrs);
752731

732+
let has_default = default.is_some();
753733
if let Some(default) = default {
754-
field_read = quote::quote! {&{
734+
field_read = quote::quote! {{
755735
asn1::to_optional_default(#field_read, &(#default).into())
756736
}}
757737
}
@@ -761,12 +741,12 @@ fn generate_write_element(
761741
let value = arg.value;
762742
if arg.required {
763743
quote::quote_spanned! {f.span() =>
764-
w.write_element(&asn1::Explicit::<_, #value>::new(#field_read))?;
744+
w.write_element(asn1::Explicit::<_, #value>::from_ref(#field_read))?;
765745
}
766746
} else {
767747
quote::quote_spanned! {f.span() =>
768748
if let Some(v) = #field_read {
769-
w.write_element(&asn1::Explicit::<_, #value>::new(v))?;
749+
w.write_element(asn1::Explicit::<_, #value>::from_ref(v))?;
770750
}
771751
}
772752
}
@@ -775,12 +755,12 @@ fn generate_write_element(
775755
let value = arg.value;
776756
if arg.required {
777757
quote::quote_spanned! {f.span() =>
778-
w.write_element(&asn1::Implicit::<_, #value>::new(#field_read))?;
758+
w.write_element(asn1::Implicit::<_, #value>::from_ref(#field_read))?;
779759
}
780760
} else {
781761
quote::quote_spanned! {f.span() =>
782762
if let Some(v) = #field_read {
783-
w.write_element(&asn1::Implicit::<_, #value>::new(v))?;
763+
w.write_element(asn1::Implicit::<_, #value>::from_ref(v))?;
784764
}
785765
}
786766
}
@@ -790,6 +770,12 @@ fn generate_write_element(
790770
quote::quote! {
791771
w.write_element(asn1::writable_defined_by_item(#defined_by_marker_read))?;
792772
}
773+
} else if has_default {
774+
quote::quote! {
775+
if let Some(v) = #field_read {
776+
w.write_element(v)?;
777+
}
778+
}
793779
} else {
794780
quote::quote! {
795781
w.write_element(#field_read)?;
@@ -870,13 +856,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma
870856
OpType::Explicit(arg) => {
871857
let tag = arg.value;
872858
quote::quote! {
873-
#name::#ident(value) => w.write_element(&asn1::Explicit::<_, #tag>::new(value)),
859+
#name::#ident(value) => w.write_element(asn1::Explicit::<_, #tag>::from_ref(value)),
874860
}
875861
}
876862
OpType::Implicit(arg) => {
877863
let tag = arg.value;
878864
quote::quote! {
879-
#name::#ident(value) => w.write_element(&asn1::Implicit::<_, #tag>::new(value)),
865+
#name::#ident(value) => w.write_element(asn1::Implicit::<_, #tag>::from_ref(value)),
880866
}
881867
}
882868
OpType::DefinedBy(_) => panic!("Can't use #[defined_by] in an Asn1Write on an enum"),

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
2-
#![forbid(unsafe_code)]
32
#![deny(rust_2018_idioms)]
43

54
//! This crate provides you with the ability to generate and parse ASN.1

src/types.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ impl<T: SimpleAsn1Writable> Asn1Writable for T {
8282
}
8383
}
8484

85-
impl<T: SimpleAsn1Writable> SimpleAsn1Writable for &T {
86-
const TAG: Tag = T::TAG;
87-
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
88-
T::write_data(self, dest)
89-
}
90-
}
91-
9285
impl<T: SimpleAsn1Writable> SimpleAsn1Writable for Box<T> {
9386
const TAG: Tag = T::TAG;
9487
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
@@ -145,13 +138,6 @@ impl Asn1Writable for Tlv<'_> {
145138
}
146139
}
147140

148-
impl Asn1Writable for &Tlv<'_> {
149-
#[inline]
150-
fn write(&self, w: &mut Writer<'_>) -> WriteResult {
151-
Tlv::write(self, w)
152-
}
153-
}
154-
155141
/// The ASN.1 NULL type, for use with `Parser.read_element` and
156142
/// `Writer.write_element`.
157143
pub type Null = ();
@@ -1734,6 +1720,7 @@ impl<T: Asn1Writable, V: Borrow<[T]>> SimpleAsn1Writable for SetOfWriter<'_, T,
17341720
/// `Implicit` is a type which wraps another ASN.1 type, indicating that the tag is an ASN.1
17351721
/// `IMPLICIT`. This will generally be used with `Option` or `Choice`.
17361722
#[derive(PartialEq, Eq, Debug)]
1723+
#[repr(transparent)]
17371724
pub struct Implicit<T, const TAG: u32> {
17381725
inner: T,
17391726
}
@@ -1743,6 +1730,10 @@ impl<T, const TAG: u32> Implicit<T, { TAG }> {
17431730
Implicit { inner: v }
17441731
}
17451732

1733+
pub fn from_ref(v: &T) -> &Self {
1734+
unsafe { &*(v as *const T as *const Self) }
1735+
}
1736+
17461737
pub fn as_inner(&self) -> &T {
17471738
&self.inner
17481739
}
@@ -1778,6 +1769,7 @@ impl<T: SimpleAsn1Writable, const TAG: u32> SimpleAsn1Writable for Implicit<T, {
17781769
/// `Explicit` is a type which wraps another ASN.1 type, indicating that the tag is an ASN.1
17791770
/// `EXPLICIT`. This will generally be used with `Option` or `Choice`.
17801771
#[derive(PartialEq, Eq, Debug)]
1772+
#[repr(transparent)]
17811773
pub struct Explicit<T, const TAG: u32> {
17821774
inner: T,
17831775
}
@@ -1787,6 +1779,10 @@ impl<T, const TAG: u32> Explicit<T, { TAG }> {
17871779
Explicit { inner: v }
17881780
}
17891781

1782+
pub fn from_ref(v: &T) -> &Self {
1783+
unsafe { &*(v as *const T as *const Self) }
1784+
}
1785+
17901786
pub fn as_inner(&self) -> &T {
17911787
&self.inner
17921788
}

0 commit comments

Comments
 (0)