Hacker News new | past | comments | ask | show | jobs | submit login

I think this article is missing a mention of #[repr(transparent)], which is supposed to help bridge the problem of orphan rules, etc. I'm not sure why there isn't a guarantee that Rust will treat a wrapped type with a single field in the same way as a naked type, but I suspect it's because they Just Haven't Gotten To It Yet.

https://www.reddit.com/r/rust/comments/kbyb6z/what_is_the_pu...

Also, the "reference" issue described in the article later is painful, but can be solved w/the Yoke or Rental crates right now. I hope those make it into the stdlib, however.




> I'm not sure why there isn't a guarantee that Rust will treat a wrapped type with a single field in the same way as a naked type, but I suspect it's because they Just Haven't Gotten To It Yet.

What do you mean by "treat it in the same way" ?

#[repr(transparent)] does promise you something with identical layout and alignment. If whatever is in there is actually the same kind of thing then the transmute will work, but the problem is - what if it's not?

Take nook::BalancedI8 and std::num::NonZeroI8 these both have the same representation as Rust's i8 built-in type, they're one byte signed integers. But, wait a minute Option<BalancedI8> and Option<NonZeroI8> also have the same representation.

Now, if that single byte is the value 9, we're fine, all five of these types agree that's just 9, no problem. Same for -106 or 35 or most other values.

But, for 0 it's a problem. i8, BalancedI8 and Option<BalancedI8> all agree that's zero. However NonZeroI8 can't conceive of this value, we've induced Undefined Behaviour, and Option<NonZeroI8> says this is None.

Similarly at -128, i8, NonZeroI8 and Option<NonZeroI8> all agree that's -128, their minimum possible value. However the minimum value of BalancedI8 is -127 so this representation is Undefined Behaviour, and in Option<BalanacedI8> that's None.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: