Skip to content

A trick to compute return type layout on dev/experimental/dyn branch #70

@zjp-CN

Description

@zjp-CN

let mut ptr = ::core::ptr::null_mut();
if false {
unsafe { *ptr = Self::#name(self, #(#args),*) };
}
fn layout_for<T>(_: *mut T) -> ::core::alloc::Layout {
::core::alloc::Layout::new::<T>()
}
layout_for(ptr)

This snippet can be simplified by calling a generic function to compute layout of the return type:

const fn return_type_layout<T, F: FnOnce() -> T>(_: &F) -> ::core::alloc::Layout {
    ::core::alloc::Layout::new::<T>()
}
return_type_layout(&|| Self::#name(self, #(#args),*))

playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions