|
230 | 230 | //!
|
231 | 231 | //! # Representation
|
232 | 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: |
| 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 must meet |
| 236 | +//! all of the following conditions: |
237 | 237 | //! * Is a zero-sized type with alignment 1 (a "1-ZST").
|
238 | 238 | //! * Has no fields.
|
239 | 239 | //! * Does not have the `#[non_exhaustive]` attribute.
|
240 | 240 | //!
|
| 241 | +//! If that is the case, then `Result<T, E>` has the same size, alignment, and [function call ABI] |
| 242 | +//! as `I` (and therefore, as `Option<I>`). If `I` is `T`, it is therefore sound to transmute `t: I` |
| 243 | +//! to `Result<T, E>` (which will produce `Ok(t)`), and to transmute `Ok(t): Result<T, E>` to `I` |
| 244 | +//! (which will produce `t`). If `I` is `E`, the same applies with `Ok` replaced by `Err`. |
| 245 | +//! |
241 | 246 | //! For example, `NonZeroI32` qualifies for the `Option` representation
|
242 | 247 | //! guarantees, and `()` is a zero-sized type with alignment 1, no fields, and
|
243 | 248 | //! 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: |
| 249 | +//! `Result<(), NonZeroI32>` have the same size, alignment, and ABI |
| 250 | +//! as `NonZeroI32` (and `Option<NonZeroI32>`). The only difference is the implied semantics: |
246 | 251 | //! * `Option<NonZeroI32>` is "a non-zero i32 might be present"
|
247 | 252 | //! * `Result<NonZeroI32, ()>` is "a non-zero i32 success result, if any"
|
248 | 253 | //! * `Result<(), NonZeroI32>` is "a non-zero i32 error result, if any"
|
249 | 254 | //!
|
250 | 255 | //! [opt-rep]: ../option/index.html#representation "Option Representation"
|
| 256 | +//! [function call ABI]: ../primitive.fn.html#abi-compatibility |
251 | 257 | //!
|
252 | 258 | //! # Method overview
|
253 | 259 | //!
|
|
0 commit comments