Skip to content

Commit e32ce84

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

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, false, &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

@@ -454,7 +452,6 @@ fn add_bounds(
454452
field_types: Vec<(syn::Type, OpType, bool)>,
455453
bound: syn::TypeParamBound,
456454
defined_by_bound: syn::TypeParamBound,
457-
add_ref: bool,
458455
) {
459456
let where_clause = if field_types.is_empty() {
460457
return;
@@ -468,11 +465,11 @@ fn add_bounds(
468465
};
469466

470467
for (f, op_type, has_default) in field_types {
471-
let (bounded_ty, required_bound) = match (op_type, add_ref) {
472-
(OpType::Regular, _) => (f, bound.clone()),
473-
(OpType::DefinedBy(_), _) => (f, defined_by_bound.clone()),
468+
let (bounded_ty, required_bound) = match op_type {
469+
OpType::Regular => (f, bound.clone()),
470+
OpType::DefinedBy(_) => (f, defined_by_bound.clone()),
474471

475-
(OpType::Implicit(OpTypeArgs { value, required }), false) => {
472+
OpType::Implicit(OpTypeArgs { value, required }) => {
476473
let ty = if required || has_default {
477474
syn::parse_quote!(asn1::Implicit::<#f, #value>)
478475
} else {
@@ -481,32 +478,14 @@ fn add_bounds(
481478

482479
(ty, bound.clone())
483480
}
484-
(OpType::Implicit(OpTypeArgs { value, required }), true) => {
485-
let ty = if required || has_default {
486-
syn::parse_quote!(for<'asn1_internal> asn1::Implicit::<&'asn1_internal #f, #value>)
487-
} else {
488-
syn::parse_quote!(for<'asn1_internal> asn1::Implicit::<&'asn1_internal <#f as asn1::OptionExt>::T, #value>)
489-
};
490481

491-
(ty, bound.clone())
492-
}
493-
494-
(OpType::Explicit(OpTypeArgs { value, required }), false) => {
482+
OpType::Explicit(OpTypeArgs { value, required }) => {
495483
let ty = if required || has_default {
496484
syn::parse_quote!(asn1::Explicit::<#f, #value>)
497485
} else {
498486
syn::parse_quote!(asn1::Explicit::<<#f as asn1::OptionExt>::T, #value>)
499487
};
500488

501-
(ty, bound.clone())
502-
}
503-
(OpType::Explicit(OpTypeArgs { value, required }), true) => {
504-
let ty = if required || has_default {
505-
syn::parse_quote!(for<'asn1_internal> asn1::Explicit::<&'asn1_internal #f, #value>)
506-
} else {
507-
syn::parse_quote!(for<'asn1_internal> asn1::Explicit::<&'asn1_internal <#f as asn1::OptionExt>::T, #value>)
508-
};
509-
510489
(ty, bound.clone())
511490
}
512491
};
@@ -814,8 +793,9 @@ fn generate_write_element(
814793
) -> proc_macro2::TokenStream {
815794
let (write_type, default) = extract_field_properties(&f.attrs);
816795

796+
let has_default = default.is_some();
817797
if let Some(default) = default {
818-
field_read = quote::quote! {&{
798+
field_read = quote::quote! {{
819799
asn1::to_optional_default(#field_read, &(#default).into())
820800
}}
821801
}
@@ -825,12 +805,12 @@ fn generate_write_element(
825805
let value = arg.value;
826806
if arg.required {
827807
quote::quote_spanned! {f.span() =>
828-
w.write_element(&asn1::Explicit::<_, #value>::new(#field_read))?;
808+
w.write_element(asn1::Explicit::<_, #value>::from_ref(#field_read))?;
829809
}
830810
} else {
831811
quote::quote_spanned! {f.span() =>
832812
if let Some(v) = #field_read {
833-
w.write_element(&asn1::Explicit::<_, #value>::new(v))?;
813+
w.write_element(asn1::Explicit::<_, #value>::from_ref(v))?;
834814
}
835815
}
836816
}
@@ -839,12 +819,12 @@ fn generate_write_element(
839819
let value = arg.value;
840820
if arg.required {
841821
quote::quote_spanned! {f.span() =>
842-
w.write_element(&asn1::Implicit::<_, #value>::new(#field_read))?;
822+
w.write_element(asn1::Implicit::<_, #value>::from_ref(#field_read))?;
843823
}
844824
} else {
845825
quote::quote_spanned! {f.span() =>
846826
if let Some(v) = #field_read {
847-
w.write_element(&asn1::Implicit::<_, #value>::new(v))?;
827+
w.write_element(asn1::Implicit::<_, #value>::from_ref(v))?;
848828
}
849829
}
850830
}
@@ -854,6 +834,12 @@ fn generate_write_element(
854834
quote::quote! {
855835
w.write_element(asn1::writable_defined_by_item(#defined_by_marker_read))?;
856836
}
837+
} else if has_default {
838+
quote::quote! {
839+
if let Some(v) = #field_read {
840+
w.write_element(v)?;
841+
}
842+
}
857843
} else {
858844
quote::quote! {
859845
w.write_element(#field_read)?;
@@ -934,13 +920,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma
934920
OpType::Explicit(arg) => {
935921
let tag = arg.value;
936922
quote::quote! {
937-
#name::#ident(value) => w.write_element(&asn1::Explicit::<_, #tag>::new(value)),
923+
#name::#ident(value) => w.write_element(asn1::Explicit::<_, #tag>::from_ref(value)),
938924
}
939925
}
940926
OpType::Implicit(arg) => {
941927
let tag = arg.value;
942928
quote::quote! {
943-
#name::#ident(value) => w.write_element(&asn1::Implicit::<_, #tag>::new(value)),
929+
#name::#ident(value) => w.write_element(asn1::Implicit::<_, #tag>::from_ref(value)),
944930
}
945931
}
946932
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)