Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It is not clear, does compiler explicitly knows about properties of `NonNull` (for example, that it has neutral value to collapse `Option<NonNull<T>>`), so it is part of compiler, or it is expressed in type system, so it is part of standard library?

Same question about other «signal» types.



> part of compiler, or it is expressed in type system, so it is part of standard library

compiler + standard library (core library to be more specific)

`NonNull` is mostly "just" a library type in the core libary, but it uses two unstable/nightly attributes to tell the compiler about it's special properties (see other answers, also to be clear its not a `lang_item` attribute but something other types can use, too).

The special thing about core/std in rust is that due to it being part of the specific compiler version it can use (some, careful considered) subset of unstable features as long as they work for that specific use case and don't "leak"/accidentally stabilize through it.

But this attributes are in progress of getting replaces with a new mechanism of pattern annotations. I.e. NonNull will be annotated with attribute which contains a `1..` pattern (i.e. value is 1 or larger) but other patterns are getting support, too. (The second attribute to enforce/guarantee niche optimizations might still be needed for stability guarantees.)

And I think??? there are plans to stabilize this new mechanism, but I'm absolutely not sure what the state of this is (RFC accepted?, stabilization open/likely/decided? implementation done/partial?). I'm currently not in the loop.

So in the long future you probably can write your own types with other niches e.g. a u8 with `..127` (MSB unused).


For anyone wondering, NonNull (along with other "constrained" types like NonZero integers) uses internal compiler attributes (https://doc.rust-lang.org/src/core/ptr/non_null.rs.html#72):

  #[rustc_layout_scalar_valid_range_start(1)]
This gives rustc enough information to perform niche optimizations such as collapsing Option<NonNull<T>>.

You can technically use those for your own types, but it requires nightly, obviously.


However, Rust does automatically provide a niche if you make a simple enumeration which doesn't occupy all bit patterns and we don't need compiler-only features, or even unstable Rust, this Just Works™.

If you have a USHoliday enum, with values like Juneteenth the fact is there's not that many US national holidays, so both USHoliday and Option<USHoliday> will be the same size, one byte with no extra work.


It's a mix of both. https://doc.rust-lang.org/src/core/ptr/non_null.rs.html#72-7...

  #[rustc_layout_scalar_valid_range_start(1)]
  #[rustc_nonnull_optimization_guaranteed]
This declares that NonNull cannot be outside of the range (1..). These magic attributes are only allowed to be used by the stdlib, and they're direct compiler instructions. Nonnull is special because the optimisation is guaranteed so the compiler does have special code to handle it, but other types can be optimised without compiler changes.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: