Skip to content

Commit 73f5fe7

Browse files
committed
Revise Result/Option guarantee docs
The notation used here (e.g. "transmute `t: T` to `Option<T>`") felt maybe a bit heavy, in the context of the library documentation, so let's elaborate this a bit. Also, in the `Option` docs, we talk about this being true for "the following types `T`", but it felt this caveat might get a bit lost in the next sentence that talks about the valid transmutations, so let's reiterate the caveat. While we're touching the line, we can improve: > The only difference is the implied semantics: This sentence was a bit awkward due to the mismatched plurality and not identifying the difference being spoken to. We'll reword this to make it more clear. We'll wrap to 80, since the existing text and most of the doc comments in these files are wrapped this way.
1 parent 058f08d commit 73f5fe7

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

library/core/src/option.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@
118118
//!
119119
//! # Representation
120120
//!
121-
//! Rust guarantees to optimize the following types `T` such that
122-
//! [`Option<T>`] has the same size, alignment, and [function call ABI] as `T`.
123-
//! It is therefore sound to transmute `t: T` to `Option<T>` (which will produce `Some(t)`), and
124-
//! to transmute `Some(t): Option<T>` to `T` (which will produce `t`).
121+
//! Rust guarantees to optimize the following types `T` such that [`Option<T>`]
122+
//! has the same size, alignment, and [function call ABI] as `T`. It is
123+
//! therefore sound, when `T` is one of these types, to transmute a value `t` of
124+
//! type `T` to type `Option<T>` (producing the value `Some(t)`) and to
125+
//! transmute a value `Some(t)` of type `Option<T>` to type `T` (producing the
126+
//! value `t`).
125127
//!
126128
//! In some of these cases, Rust further guarantees the following:
127129
//! - `transmute::<_, Option<T>>([0u8; size_of::<T>()])` is sound and produces

library/core/src/result.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,25 @@
230230
//!
231231
//! # Representation
232232
//!
233-
//! In some cases, [`Result<T, E>`] comes with size, alignment, and ABI guarantees.
234-
//! Specifically, one of either the `T` or `E` type must be a type that qualifies for the `Option`
235-
//! [representation guarantees][opt-rep] (let's call that type `I`), and the *other* type
236-
//! is a zero-sized type with alignment 1 (a "1-ZST").
237-
//!
238-
//! If that is the case, then `Result<T, E>` has the same size, alignment, and [function call ABI]
239-
//! as `I` (and therefore, as `Option<I>`). If `I` is `T`, it is therefore sound to transmute `t: I`
240-
//! to `Result<T, E>` (which will produce `Ok(t)`), and to transmute `Ok(t): Result<T, E>` to `I`
241-
//! (which will produce `t`). If `I` is `E`, the same applies with `Ok` replaced by `Err`.
242-
//!
243-
//! For example, `NonZeroI32` qualifies for the `Option` representation guarantees, and `()` is a
244-
//! zero-sized type with alignment 1. This means that both `Result<NonZeroI32, ()>` and
245-
//! `Result<(), NonZeroI32>` have the same size, alignment, and ABI
246-
//! as `NonZeroI32` (and `Option<NonZeroI32>`). The only difference is the implied semantics:
233+
//! In some cases, [`Result<T, E>`] comes with size, alignment, and ABI
234+
//! guarantees. Specifically, one of either the `T` or `E` type must be a type
235+
//! that qualifies for the `Option` [representation guarantees][opt-rep] (let's
236+
//! call that type `I`), and the *other* type is a zero-sized type with
237+
//! alignment 1 (a "1-ZST").
238+
//!
239+
//! If that is the case, then `Result<T, E>` has the same size, alignment, and
240+
//! [function call ABI] as `I` (and therefore, as `Option<I>`). If `I` is `T`,
241+
//! it is therefore sound to transmute a value `t` of type `I` to type
242+
//! `Result<T, E>` (producing the value `Ok(t)`) and to transmute a value
243+
//! `Ok(t)` of type `Result<T, E>` to type `I` (producing the value `t`). If `I`
244+
//! is `E`, the same applies with `Ok` replaced by `Err`.
245+
//!
246+
//! For example, `NonZeroI32` qualifies for the `Option` representation
247+
//! guarantees and `()` is a zero-sized type with alignment 1. This means that
248+
//! both `Result<NonZeroI32, ()>` and `Result<(), NonZeroI32>` have the same
249+
//! size, alignment, and ABI as `NonZeroI32` (and `Option<NonZeroI32>`). The
250+
//! only difference between these is in the implied semantics:
251+
//!
247252
//! * `Option<NonZeroI32>` is "a non-zero i32 might be present"
248253
//! * `Result<NonZeroI32, ()>` is "a non-zero i32 success result, if any"
249254
//! * `Result<(), NonZeroI32>` is "a non-zero i32 error result, if any"

0 commit comments

Comments
 (0)