Skip to content

Commit

Permalink
Revert "This also does not work"
Browse files Browse the repository at this point in the history
This reverts commit 3e269a7.
  • Loading branch information
someguynamedjosh committed May 14, 2023
1 parent 3e269a7 commit 13b2af8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
10 changes: 10 additions & 0 deletions examples/src/ok_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ fn try_new_recover_heads() {
}
}

#[test]
fn into_heads() {
let bar = BoxAndRefBuilder {
data: 12,
dref_builder: |data| data,
}
.build();
assert!(bar.into_heads().data == 12);
}

#[test]
fn box_and_mut_ref() {
let mut bar = BoxAndMutRefBuilder {
Expand Down
31 changes: 28 additions & 3 deletions ouroboros_macro/src/generate/into_heads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use quote::quote;

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

/// Returns the Heads struct.
pub fn make_heads(info: &StructInfo, options: Options) -> TokenStream {
/// Returns the Heads struct and a function to convert the original struct into a Heads instance.
pub fn make_into_heads(info: &StructInfo, options: Options) -> (TokenStream, TokenStream) {
let visibility = if options.do_pub_extras {
info.vis.clone()
} else {
Expand Down Expand Up @@ -51,6 +51,31 @@ pub fn make_heads(info: &StructInfo, options: Options) -> TokenStream {
#(#head_fields),*
}
};
let documentation = concat!(
"This function drops all internally referencing fields and returns only the ",
"[head fields](https://docs.rs/ouroboros/latest/ouroboros/attr.self_referencing.html#definitions) of this struct."
).to_owned();

heads_struct_def
let documentation = if !options.do_no_doc {
quote! {
#[doc=#documentation]
}
} else {
quote! { #[doc(hidden)] }
};

let generic_args = info.generic_arguments();
let into_heads_fn = quote! {
#documentation
#[allow(clippy::drop_ref)]
#[allow(clippy::drop_copy)]
#[allow(clippy::drop_non_drop)]
#visibility fn into_heads(self) -> Heads<#(#generic_args),*> {
#(#code)*
Heads {
#(#field_initializers),*
}
}
};
(heads_struct_def, into_heads_fn)
}
12 changes: 5 additions & 7 deletions ouroboros_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod utils;
use crate::{
generate::{
constructor::create_builder_and_constructor, derives::create_derives,
into_heads::make_heads, struc::create_actual_struct_def,
into_heads::make_into_heads, struc::create_actual_struct_def,
summon_checker::generate_checker_summoner,
try_constructor::create_try_builder_and_constructor, type_asserts::make_type_asserts,
with_all::make_with_all_function, with_each::make_with_functions,
Expand Down Expand Up @@ -50,15 +50,12 @@ fn self_referencing_impl(
create_try_builder_and_constructor(&info, options, BuilderType::Sync)?;
let (async_try_builder_struct_name, async_try_builder_def, async_try_constructor_def) =
create_try_builder_and_constructor(&info, options, BuilderType::Async)?;
let (
async_send_try_builder_struct_name,
async_send_try_builder_def,
async_send_try_constructor_def,
) = create_try_builder_and_constructor(&info, options, BuilderType::AsyncSend)?;
let (async_send_try_builder_struct_name, async_send_try_builder_def, async_send_try_constructor_def) =
create_try_builder_and_constructor(&info, options, BuilderType::AsyncSend)?;

let with_defs = make_with_functions(&info, options)?;
let (with_all_struct_defs, with_all_fn_defs) = make_with_all_function(&info, options)?;
let heads_struct_def = make_heads(&info, options);
let (heads_struct_def, into_heads_fn) = make_into_heads(&info, options);

let impls = create_derives(&info)?;

Expand Down Expand Up @@ -100,6 +97,7 @@ fn self_referencing_impl(
#async_send_try_constructor_def
#(#with_defs)*
#with_all_fn_defs
#into_heads_fn
}
#type_asserts_def
}
Expand Down

0 comments on commit 13b2af8

Please sign in to comment.