Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly document the size guarantees that Option makes. #75454

Merged
merged 8 commits into from
Sep 26, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@
//!
//! # Representation
//!
//! Rust guarantees to optimise the following inner types such that an [`Option`] which contains
//! them has the same size as a pointer:
//! Rust guarantees to optimize the following types `<T>` such that an
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//! Rust guarantees to optimize the following types `<T>` such that an
//! Rust guarantees to optimize the following types `T` such that an

I never saw <T> as notation for a type, not sure what that is supposed to indicate.

//! [`Option<T>`] has the same size as `T`:
//!
//! * [`Box<T>`]
//! * `&T`
//! * `&mut T`
//! * `extern "C" fn`
ltratt marked this conversation as resolved.
Show resolved Hide resolved
//! * [`num::NonZero*`]
//! * [`ptr::NonNull<T>`]
//! * `#[repr(transparent)]` struct around one of the types in this list.
//! * [`Box<T>`]
//!
//! For the above cases, it is guaranteed that one can use [`mem::transmute`]
//! between `T` and `Option<T>` and vice versa.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not vice versa! If you transmute a None from Option<T> to T, you get UB.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Should there be a guarantee for non-None in such a case, or just rule out the reverse direction entirely?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guarantee for non-None seems reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 83f47aa.

//!
//! # Examples
//!
Expand Down