[0]: https://github.com/rust-lang/rust/blob/master/library/alloc/...
[1]: https://github.com/rust-lang/rust/blob/master/library/alloc/...
However, if the type is Copy and all the bytes are zeros, then you know it can be calloc
https://crates.io/crates/misfortunate
However, Rust does have two other tricks up its sleeve:
1. Unsafe traits. You can declare a trait to be "unsafe". Implementations of this trait likewise need that "unsafe" keyword and it alerts the programmer that, as with unsafe function calls, they are responsible for actually getting the implementation details correct. Not many standard library traits are unsafe because that's a considerable burden, but a few are, including Send and Sync.
You wouldn't learn much by implementing unsafe traits wrongly in a library like misfortunate, it's the same lesson as a C++ library that scribbles on random memory addresses.
2. Rust knows whether you implemented a trait by hand or merely derived it. In a few cases it can be reasonable for the language to distinguish these cases for built-in traits, since in the former case it knows the derived trait does what is needed even though a manual implementation may not.
It is possible that future improvements to Const Generics will rely on this latter idea. So long as you derive Eq rather than implementing it, Rust can reason that your type really is suitable as a constant type parameter whereas misfortunate::Always is not suitable (it would cause mayhem if permitted because of its idea of what "equality" is) despite claiming to be Eq.
Too late to edit. This should clearly have said, in the latter case not the former case. In the hand implemented case the compiler has no idea whether your implementation has the desired properties.
unsafe trait IsZero: Copy {
fn is_zero(&self) -> bool;
}
#[derive(Copy, IsZero)]
struct Foo {
a: u8,
b: u16,
}
which could derive into unsafe impl IsZero for Foo {
fn is_zero(&self) -> bool {
true
&& self.a.is_zero()
&& self.b.is_zero()
}
}[1]: https://rust-lang.github.io/rfcs/3107-derive-enum-default.ht...
enum E<T> {
Empty = 42,
Some(NonFortyTwo<T>),
}
in the same way that the following is of size T today enum E<T> {
Empty = 0,
Some(NonZero<T>),
}
but that's not possible today.Still the existing tricks get a lot done for relatively little work. I have used Option<NonZeroUsize> to do roughly what I'd use a single integer for in C, but making explicit that zero isn't just zero, but "I dunno, invalid". Would I have done that even if it cost more space? Probably, but it was cool that I didn't even need to consider that, "Zero cost abstraction".
That's not as easy to check as one would think since Copy types can still contain uninitialized bytes.
Especially in a case like in the article, where the type is very small (one byte), and the obstacle is that the author is using a newtype.
Surely baseline is that it does something correct. Reasonable (or just "not pathologically stupid" if you separate the two, with reasonable next) comes after. To my mind an optimising compiler is a compiler first, an optimiser second.
I'm not really sure it follows, though, that you should ever expect more than that. It's nice if you can get it but the presence of a very specific optimization shouldn't be taken as the new "zero cost" baseline, and I don't think it should be taken as given that a newtype will inherit all possible optimizations of its enclosed type.