Types that are not valid when they contain certain bit patterns.
unsafe trait UndefState {
/* Required methods */
unsafe fn is_undef(val: *const Self, n: usize) -> bool
fn num() -> usize
unsafe fn set_undef(val: *mut Self, n: usize) -> ()
}| Receiver | Name | Description |
|---|---|---|
| is_undef | Checks if a storage location is in an undefined state. |
| num | The number of available undefined states. |
| set_undef | Creates an undefined state. |
This is the opposite of Pod. That is, every type is either Pod or UndefState. (But note that not all types need to implement either of those types.)
A type that implements this trait has a constant number of so-called undefined states.
Objects of type char cannot contain surrogate code points. Since char is just a wrapper of u32, the following implementation is valid and gives us 2048 undefined states:
unsafe impl UndefState for char {
fn num() -> usize { 0xE000 - 0xD800 }
unsafe fn set_undef(val: *mut char, n: usize) {
assert!(n < Self::num());
*(val as *mut u32) = n as u32 + 0xE000;
}
unsafe fn is_undef(val: *const char, n: usize) -> bool {
assert!(n < Self::num());
*(val as *const u32) == n as u32 + 0xE000
}
}