|
230 | 230 | //! |
231 | 231 | //! # Representation |
232 | 232 | //! |
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 | +//! |
247 | 252 | //! * `Option<NonZeroI32>` is "a non-zero i32 might be present" |
248 | 253 | //! * `Result<NonZeroI32, ()>` is "a non-zero i32 success result, if any" |
249 | 254 | //! * `Result<(), NonZeroI32>` is "a non-zero i32 error result, if any" |
|
0 commit comments