Skip to content

Commit 6dddc88

Browse files
Rollup merge of #124870 - Lokathor:update-result-docs, r=dtolnay
Update Result docs to the new guarantees The `Option` docs already explain the guarantees given in [RFC 3391](https://github.com/rust-lang/rfcs/blob/master/text/3391-result_ffi_guarantees.md), so all that we need is a paragraph saying that some `Result` type combinations will also qualify. Part of #110503
2 parents b582f80 + 9b480da commit 6dddc88

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

library/core/src/option.rs

+3
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,13 @@
137137
//!
138138
//! [^extern_fn]: this remains true for any argument/return types and any other ABI: `extern "abi" fn` (_e.g._, `extern "system" fn`)
139139
//!
140+
//! Under some conditions the above types `T` are also null pointer optimized when wrapped in a [`Result`][result_repr].
141+
//!
140142
//! [`Box<U>`]: ../../std/boxed/struct.Box.html
141143
//! [`num::NonZero*`]: crate::num
142144
//! [`ptr::NonNull<U>`]: crate::ptr::NonNull
143145
//! [function call ABI]: ../primitive.fn.html#abi-compatibility
146+
//! [result_repr]: crate::result#representation
144147
//!
145148
//! This is called the "null pointer optimization" or NPO.
146149
//!

library/core/src/result.rs

+21
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,27 @@
228228
//! [`Err(E)`]: Err
229229
//! [io::Error]: ../../std/io/struct.Error.html "io::Error"
230230
//!
231+
//! # Representation
232+
//!
233+
//! In some cases, [`Result<T, E>`] will gain the same size, alignment, and ABI
234+
//! guarantees as [`Option<U>`] has. One of either the `T` or `E` type must be a
235+
//! type that qualifies for the `Option` [representation guarantees][opt-rep],
236+
//! and the *other* type must meet all of the following conditions:
237+
//! * Is a zero-sized type with alignment 1 (a "1-ZST").
238+
//! * Has no fields.
239+
//! * Does not have the `#[non_exhaustive]` attribute.
240+
//!
241+
//! For example, `NonZeroI32` qualifies for the `Option` representation
242+
//! guarantees, and `()` is a zero-sized type with alignment 1, no fields, and
243+
//! it isn't `non_exhaustive`. This means that both `Result<NonZeroI32, ()>` and
244+
//! `Result<(), NonZeroI32>` have the same size, alignment, and ABI guarantees
245+
//! as `Option<NonZeroI32>`. The only difference is the implied semantics:
246+
//! * `Option<NonZeroI32>` is "a non-zero i32 might be present"
247+
//! * `Result<NonZeroI32, ()>` is "a non-zero i32 success result, if any"
248+
//! * `Result<(), NonZeroI32>` is "a non-zero i32 error result, if any"
249+
//!
250+
//! [opt-rep]: ../option/index.html#representation "Option Representation"
251+
//!
231252
//! # Method overview
232253
//!
233254
//! In addition to working with pattern matching, [`Result`] provides a

0 commit comments

Comments
 (0)