-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
75 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Implementation Details | ||
|
||
{{%segment outline}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Exercise: TBD | ||
|
||
NOTES: | ||
|
||
- maybe a good place for a linked list? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
minutes: 10 | ||
--- | ||
|
||
# Niche Optimization | ||
|
||
For some types, there are in-memory bit patterns that do not represent a valid | ||
value. For example, `bool` can only be 0 or 1, and references are represented as | ||
non-NULL pointers. Rust uses this observation to store enums without a distinct | ||
discriminant field, saving space. | ||
|
||
```rust,editable | ||
#![allow(dead_code)] | ||
use std::{mem::size_of_val, slice::from_raw_parts}; | ||
enum TriState { | ||
Set(bool), | ||
Unset, | ||
} | ||
fn show<T: Sized>(name: &str, value: T) { | ||
let bytes = unsafe { | ||
from_raw_parts(&value as *const T as *const u8, size_of_val(&value)) | ||
} | ||
.iter() | ||
.map(|b| format!("{:02x}", b)) | ||
.collect::<Vec<_>>() | ||
.join(""); | ||
println!("{}: {} = {}", name, std::any::type_name::<T>(), bytes); | ||
} | ||
fn main() { | ||
show("false", TriState::Set(false)); | ||
show("true", TriState::Set(true)); | ||
show("unset", TriState::Unset); | ||
} | ||
``` | ||
|
||
<details> | ||
|
||
The example shows Rust choosing a non-boolean value for the `Unset` variant. | ||
|
||
Try showing: | ||
|
||
- `&x` for some x | ||
- `Some(&x)` | ||
- `None::<&u32>` | ||
- `Some(Some(&x))` | ||
- `std::num::NonZero::new(10)` | ||
|
||
Null pointer optimization: For | ||
[some types](https://doc.rust-lang.org/std/option/#representation), Rust | ||
guarantees that `size_of::<T>()` equals `size_of::<Option<T>>()` and that the | ||
all-zeroes pattern transmutes to `None`. This is a special-case of the niche | ||
optimization for `Option`. | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Solution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters