Skip to content

Commit

Permalink
Fix safety of transparent_convert (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth authored Sep 9, 2024
1 parent a43baaf commit d741c84
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ pub fn transparent_convert(

let full = quote! {
#expanded
#[repr(transparent)]
#input_cached
};
proc_macro::TokenStream::from(full.to_token_stream())
Expand Down
2 changes: 2 additions & 0 deletions macro/src/transparent_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub fn gen_transparent_convert(s: ItemStruct) -> proc_macro2::TokenStream {
impl #impl_generics #struct_name #ty_generics {
// can potentially add transparent_convert_owned, _mut later
pub(crate) fn transparent_convert(from: &'transparent_convert_outer #field1) -> &'transparent_convert_outer Self {
// Safety: This is safe because the caller of gen_transparent_convert
// adds a repr(transparent) to the struct.
unsafe {
&*(from as *const #field1 as *const Self)
}
Expand Down

0 comments on commit d741c84

Please sign in to comment.