Skip to content

Commit 1ab6eb0

Browse files
committed
---
yaml --- r: 229030 b: refs/heads/try c: 42c2f10 h: refs/heads/master v: v3
1 parent 9d9af08 commit 1ab6eb0

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: aca2057ed5fb7af3f8905b2bc01f72fa001c35c8
33
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4-
refs/heads/try: 94a89e561a62d54d25dd64329ef8af396d8ec032
4+
refs/heads/try: 42c2f107c120e9da046c9b26aa34238fcd6549b6
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
66
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
77
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try/src/doc/tarpl/exotic-sizes.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,33 @@ consider passing in `0` as Undefined Behaviour.
8181

8282
Rust also enables types to be declared that *cannot even be instantiated*. These
8383
types can only be talked about at the type level, and never at the value level.
84+
Empty types can be declared by specifying an enum with no variants:
8485

8586
```rust
86-
enum Foo { } // No variants = EMPTY
87+
enum Void {} // No variants = EMPTY
8788
```
8889

89-
TODO: WHY?!
90+
Empty types are even more marginal than ZSTs. The primary motivating example for
91+
Void types is type-level unreachability. For instance, suppose an API needs to
92+
return a Result in general, but a specific case actually is infallible. It's
93+
actually possible to communicate this at the type level by returning a
94+
`Result<T, Void>`. Consumers of the API can confidently unwrap such a Result
95+
knowing that it's *statically impossible* for this value to be an `Err`, as
96+
this would require providing a value of type Void.
97+
98+
In principle, Rust can do some interesting analysees and optimizations based
99+
on this fact. For instance, `Result<T, Void>` could be represented as just `T`,
100+
because the Err case doesn't actually exist. Also in principle the following
101+
could compile:
102+
103+
```rust,ignore
104+
enum Void {}
105+
106+
let res: Result<u32, Void> = Ok(0);
107+
108+
// Err doesn't exist anymore, so Ok is actually irrefutable.
109+
let Ok(num) = res;
110+
```
111+
112+
But neither of these tricks work today, so all Void types get you today is
113+
the ability to be confident that certain situations are statically impossible.

0 commit comments

Comments
 (0)