Skip to content

Commit

Permalink
Revert "Blah."
Browse files Browse the repository at this point in the history
This reverts commit 3063769.
  • Loading branch information
someguynamedjosh committed Jun 11, 2023
1 parent 3063769 commit 71de8eb
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 123 deletions.
92 changes: 48 additions & 44 deletions examples/src/ok_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use core::fmt::Debug;
use ouroboros::macro_help::AliasableBox;

use ouroboros::self_referencing;

Expand Down Expand Up @@ -50,32 +54,32 @@ struct AutoDetectCovarianceOnFieldsWithoutThis {
self_reference: &'this (),
}

// /// This test just makes sure that the macro copes with a ton of template parameters being thrown at
// /// it, specifically checking that the templates work fine even when a generated struct doesn't need
// /// all of them. (E.G. heads will only contain 'd, A, and B.)
// #[self_referencing]
// struct TemplateMess<'d, A, B: 'static, C: 'static>
// where
// A: ?Sized,
// B: 'static,
// C: 'static,
// {
// external: &'d A,
// data1: B,
// #[borrows(data1)]
// data2: &'this C,
// data3: B,
// #[borrows(mut data3)]
// data4: &'this mut C,
// }
/// This test just makes sure that the macro copes with a ton of template parameters being thrown at
/// it, specifically checking that the templates work fine even when a generated struct doesn't need
/// all of them. (E.G. heads will only contain 'd, A, and B.)
#[self_referencing]
struct TemplateMess<'d, A, B: 'static, C: 'static>
where
A: ?Sized,
B: 'static,
C: 'static,
{
external: &'d A,
data1: B,
#[borrows(data1)]
data2: &'this C,
data3: B,
#[borrows(mut data3)]
data4: &'this mut C,
}

// /// Regression test for #46
// #[self_referencing]
// struct PreviouslyBrokeAutoGeneratedChecker<T: 'static> {
// x: T,
// #[borrows(mut x)]
// y: &'this (),
// }
/// Regression test for #46
#[self_referencing]
struct PreviouslyBrokeAutoGeneratedChecker<T: 'static> {
x: T,
#[borrows(mut x)]
y: &'this (),
}

#[test]
fn box_and_ref() {
Expand Down Expand Up @@ -200,25 +204,25 @@ fn box_and_mut_ref() {
assert!(bar.with_dref(|dref| **dref) == 34);
}

// #[test]
// fn template_mess() {
// let ext_str = "Hello World!".to_owned();
// let mut instance = TemplateMessBuilder {
// external: &ext_str[..],
// data1: "asdf".to_owned(),
// data2_builder: |data1_contents| data1_contents,
// data3: "asdf".to_owned(),
// data4_builder: |data3_contents| data3_contents,
// }
// .build();
// instance.with_external(|ext| assert_eq!(*ext, "Hello World!"));
// instance.with_data1(|data| assert_eq!(data, "asdf"));
// instance.with_data4_mut(|con| **con = "Modified".to_owned());
// instance.with(|fields| {
// assert!(**fields.data1 == **fields.data2);
// assert!(*fields.data4 == "Modified");
// });
// }
#[test]
fn template_mess() {
let ext_str = "Hello World!".to_owned();
let mut instance = TemplateMessBuilder {
external: &ext_str[..],
data1: "asdf".to_owned(),
data2_builder: |data1_contents| data1_contents,
data3: "asdf".to_owned(),
data4_builder: |data3_contents| data3_contents,
}
.build();
instance.with_external(|ext| assert_eq!(*ext, "Hello World!"));
instance.with_data1(|data| assert_eq!(data, "asdf"));
instance.with_data4_mut(|con| **con = "Modified".to_owned());
instance.with(|fields| {
assert!(**fields.data1 == **fields.data2);
assert!(*fields.data4 == "Modified");
});
}

const STATIC_INT: i32 = 456;
#[test]
Expand Down
11 changes: 5 additions & 6 deletions ouroboros_macro/src/generate/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub fn create_builder_and_constructor(
) -> Result<(Ident, TokenStream, TokenStream), Error> {
let struct_name = info.ident.clone();
let generic_args = info.generic_arguments();
let generic_args_with_static_lifetimes = info.generic_arguments_with_static_lifetimes();

let vis = if options.do_pub_extras {
info.vis.clone()
Expand Down Expand Up @@ -160,16 +159,16 @@ pub fn create_builder_and_constructor(
#documentation
#vis #constructor_fn(#(#params),*) -> #struct_name <#(#generic_args),*> {
::ouroboros::macro_help::const_assert_eq!(
::core::mem::size_of::<#struct_name<#(#generic_args_with_static_lifetimes),*>>(),
::core::mem::size_of::<#internal_ident<#(#generic_args_with_static_lifetimes),*>>()
::core::mem::size_of::<#struct_name<#(#generic_args),*>>(),
::core::mem::size_of::<#internal_ident<#(#generic_args),*>>()
);
::ouroboros::macro_help::const_assert_eq!(
::core::mem::align_of::<#struct_name<#(#generic_args_with_static_lifetimes),*>>(),
::core::mem::align_of::<#internal_ident<#(#generic_args_with_static_lifetimes),*>>()
::core::mem::align_of::<#struct_name<#(#generic_args),*>>(),
::core::mem::align_of::<#internal_ident<#(#generic_args),*>>()
);
#(#code)*
unsafe {
::core::mem::transmute(#internal_ident::<#(#generic_args),*> {
::core::mem::transmute(#internal_ident<#(#generic_args),*> {
#(#field_names),*
})
}
Expand Down
28 changes: 0 additions & 28 deletions ouroboros_macro/src/generate/drop.rs

This file was deleted.

2 changes: 1 addition & 1 deletion ouroboros_macro/src/generate/into_heads.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream;
use quote::quote;
use quote::{format_ident, quote};

use crate::info_structures::{Options, StructInfo};

Expand Down
1 change: 0 additions & 1 deletion ouroboros_macro/src/generate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod constructor;
pub mod derives;
pub mod drop;
pub mod into_heads;
pub mod struc;
pub mod summon_checker;
Expand Down
6 changes: 3 additions & 3 deletions ouroboros_macro/src/generate/struc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ pub fn create_actual_struct_def(info: &StructInfo) -> Result<TokenStream, Error>
fields.push(quote! { #ident: ::core::marker::PhantomData<#ty> });
}
let generic_params = info.generic_params();
let generic_args = info.generic_arguments_with_static_lifetimes();
let generic_args = info.generic_arguments();
let generic_where = &info.generics.where_clause;
let ident = &info.ident;
let internal_ident = &info.internal_ident;
Ok(quote! {
#visibility struct #ident <#generic_params> #generic_where {
actual_data: [u8; ::core::mem::size_of::<#internal_ident<#(#generic_args),*>>()],
_alignment: [#internal_ident<#(#generic_args),*>; 0],
#(#fields),*
}
})
}
Expand All @@ -34,6 +33,7 @@ pub fn create_actual_struct_def(info: &StructInfo) -> Result<TokenStream, Error>
/// but references *created* inside a function can be considered invalid
/// whenever, even during the duration of the function.)
pub fn create_internal_struct_def(info: &StructInfo) -> Result<TokenStream, Error> {
let vis = quote! { pub(super) };
let ident = &info.internal_ident;
let generics = &info.generics;

Expand All @@ -60,7 +60,7 @@ pub fn create_internal_struct_def(info: &StructInfo) -> Result<TokenStream, Erro
where_clause = quote! { #clause };
}
let def = quote! {
struct #ident #generics #where_clause {
#vis struct #ident #generics #where_clause {
#(#field_defs),*
}
};
Expand Down
24 changes: 1 addition & 23 deletions ouroboros_macro/src/info_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,6 @@ impl StructInfo {
&self.generics.params
}

pub fn generic_params_without_lifetimes(&self) -> Vec<&GenericParam> {
self.generics
.params
.iter()
.filter(|p| {
if let GenericParam::Lifetime(..) = p {
false
} else {
true
}
})
.collect()
}

/// Same as generic_params but with 'this and 'outer_borrow prepended.
pub fn borrowed_generic_params(&self) -> TokenStream {
if self.generic_params().is_empty() {
Expand All @@ -129,15 +115,7 @@ impl StructInfo {
}

pub fn generic_arguments(&self) -> Vec<TokenStream> {
make_generic_arguments(self.generics.params.iter().collect())
}

pub fn generic_arguments_with_static_lifetimes(&self) -> Vec<TokenStream> {
let mut result = make_generic_arguments(self.generic_params_without_lifetimes());
for _ in 0..self.generics.lifetimes().count() {
result.insert(0, quote! { 'static });
}
result
make_generic_arguments(&self.generics)
}

/// Same as generic_arguments but with 'outer_borrow and 'this prepended.
Expand Down
4 changes: 1 addition & 3 deletions ouroboros_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
info_structures::Options,
parse::parse_struct,
};
use generate::{struc::create_actual_struct_def, drop::create_drop_impl};
use generate::struc::create_actual_struct_def;
use inflector::Inflector;
use info_structures::BuilderType;
use proc_macro::TokenStream;
Expand All @@ -39,7 +39,6 @@ fn self_referencing_impl(

let actual_struct_def = create_actual_struct_def(&info)?;
let internal_struct_def = create_internal_struct_def(&info)?;
let drop_impl = create_drop_impl(&info)?;

let borrowchk_summoner = generate_checker_summoner(&info)?;

Expand Down Expand Up @@ -82,7 +81,6 @@ fn self_referencing_impl(
#[doc="The self-referencing struct."]
#actual_struct_def
#internal_struct_def
#drop_impl
#borrowchk_summoner
#builder_def
#async_builder_def
Expand Down
12 changes: 0 additions & 12 deletions ouroboros_macro/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,6 @@ fn parse_derive_attribute(attr: &Attribute) -> Result<Vec<Derive>, Error> {
pub fn parse_struct(def: &ItemStruct) -> Result<StructInfo, Error> {
let vis = def.vis.clone();
let generics = def.generics.clone();
if let Some(first_param) = generics.type_params().next() {
return Err(Error::new(
first_param.span(),
"Self-referencing structs currently cannot have type or constant parameters, only lifetime parameters.",
));
}
if let Some(first_param) = generics.const_params().next() {
return Err(Error::new(
first_param.span(),
"Self-referencing structs currently cannot have type or constant parameters, only lifetime parameters.",
));
}
let mut actual_struct_def = def.clone();
actual_struct_def.vis = vis.clone();
let mut fields = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions ouroboros_macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub fn make_generic_consumers(generics: &Generics) -> impl Iterator<Item = (Toke
}

// Takes the generics parameters from the original struct and turns them into arguments.
pub fn make_generic_arguments(generics: Vec<&GenericParam>) -> Vec<TokenStream> {
pub fn make_generic_arguments(generics: &Generics) -> Vec<TokenStream> {
let mut arguments = Vec::new();
for generic in generics {
for generic in generics.params.clone() {
match generic {
GenericParam::Type(typ) => {
let ident = &typ.ident;
Expand Down

0 comments on commit 71de8eb

Please sign in to comment.