|
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: |
237 | | -//! * Is a zero-sized type with alignment 1 (a "1-ZST"). |
238 | | -//! * Has no fields. |
239 | | -//! * Does not have the `#[non_exhaustive]` attribute. |
| 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`. |
240 | 245 | //! |
241 | 246 | //! 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: |
| 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 | +//! |
246 | 252 | //! * `Option<NonZeroI32>` is "a non-zero i32 might be present" |
247 | 253 | //! * `Result<NonZeroI32, ()>` is "a non-zero i32 success result, if any" |
248 | 254 | //! * `Result<(), NonZeroI32>` is "a non-zero i32 error result, if any" |
249 | 255 | //! |
250 | 256 | //! [opt-rep]: ../option/index.html#representation "Option Representation" |
| 257 | +//! [function call ABI]: ../primitive.fn.html#abi-compatibility |
251 | 258 | //! |
252 | 259 | //! # Method overview |
253 | 260 | //! |
|
0 commit comments