Skip to content

Null fat pointers #66316

Closed
Closed
@elichai

Description

@elichai

Hi,
Currently if you want a null fat pointer you need to do something like this:

ptr::null_mut::<[u8;0]>() as *mut [u8]

which isn't fun and take a while to find.
I see 2 ways of implementing this in libcore:
1:

pub const fn null_mut_fat<T>() -> *mut [T] {
    null_mut::<[T;0]>() as *mut [T] 
}

pub const fn null_fat<T>() -> *const [T] {
    null::<[T;0]>() as *const [T] 
}
  1. (Which I think is better because it doesn't bloat more functions)
pub fn null<T: ?Sized>() -> *mut T {
    unsafe { MaybeUninit::zeroed().assume_init() }
}

The problem is that the second currently isn't possible in a const fn. it requires making both zeroed() and assume_init() const fns, which is physically possible (by making ~4-5 functions const fn, the only problem there is the debug_assert in write_bytes)

But I don't know what's the safety consequences of using intrinsics in const fn.
If it is safe I can start PRing some of these functions to be const fn.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions