-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Expand size_of docs #44648
Expand size_of docs #44648
Conversation
At the current state of this PR, I've more questions than I have content, so consider this more like an issue than a PR at the moment. The wording in I don't like the usage of Are there any other structs that we guarantee the size for? |
src/libcore/mem.rs
Outdated
/// &T | pointer size | ||
/// &UT | 2 * pointer size | ||
/// Option<&T> | pointer size | ||
/// Option<&U> | 2 * pointer size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small nit: Option<&UT>
src/libcore/mem.rs
Outdated
@@ -180,6 +180,31 @@ pub fn forget<T>(t: T) { | |||
/// More specifically, this is the offset in bytes between successive | |||
/// items of the same type, including alignment padding. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're here, consider mentioning that this is "successive elements in an array with that item type", since layout optimization can (in theory at least) change the layout of "successive items of the same type" in a struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/libstd/primitive_docs.rs
Outdated
@@ -710,6 +710,10 @@ mod prim_u128 { } | |||
// | |||
/// The pointer-sized signed integer type. | |||
/// | |||
/// The size of this primitive is how many bytes it takes to reference any | |||
/// one byte in memory. For example, on a 32 bit target, this is 4 bytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "one byte" here distracted me, though perhaps only in context of reviewing something about sizes. Perhaps a phrasing like "to reference any location in memory"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I change it to this, I have to also describe what a location in memory is, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, *T::offset
talks of "allocated object", so maybe use that instead of location? Though I don't know if it's defined anywhere (other than LLVM) either...
src/libcore/mem.rs
Outdated
@@ -180,6 +180,31 @@ pub fn forget<T>(t: T) { | |||
/// More specifically, this is the offset in bytes between successive |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps mention that it's always a multiple of align_of::<T>()
, as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what you mean by this.
src/libcore/mem.rs
Outdated
/// isize | pointer size | ||
/// [T, n] | `size_of::<T>() * n` | ||
/// &T | pointer size | ||
/// &UT | 2 * pointer size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the accepted extern types
RFC means that there will exist types that are not Sized
but are none-the-less thin pointers. And there has been discussion of extra-fat pointers for things like dyn (Debug + Add)
. So it might be better to not mention this particularly, especially since use cases for relying on it are kinda sketchy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So basically the size of &T
is generally unstable?
src/libcore/mem.rs
Outdated
/// usize | pointer size | ||
/// isize | pointer size | ||
/// [T, n] | `size_of::<T>() * n` | ||
/// &T | pointer size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably have *const T
and *mut T
as well.
src/libcore/mem.rs
Outdated
/// items of the same type, including alignment padding. | ||
/// More specifically, this is the offset in bytes between successive elements | ||
/// in an array with that item type including alignment padding. Thus, for any | ||
/// type `T` and length `n`, [T; n] will have a size of `n * size_of<T>()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[00:03:45] tidy error: /checkout/src/libcore/mem.rs:182: trailing whitespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and size_of::<T>()
here as well
@rust-lang/libs, are you okay with what we are guaranteeing stable here? |
They seem reasonable to me. |
src/libcore/mem.rs
Outdated
/// assert_eq!(4, mem::size_of::<i32>()); | ||
/// assert_eq!(8, mem::size_of::<f64)()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo.
assert_eq!(8, mem::size_of::<f64)());
// ^ should be `>`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As kennytm wrote, there is a typo after f64.
src/libcore/mem.rs
Outdated
/// | ||
/// The types `*T`, `&T`, and `Box<T>` have the same size | ||
/// and `Option`s of `&T` and `Box<T>` have the same size as the pointer. | ||
/// Pointers to sized types will be the same size as `usize`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we say "are" instead of "will be", and "has"/"have" instead of "will have" above? The rest of this documentation is written in present tense so "will" makes it seem like those features are not implemented yet.
src/libcore/mem.rs
Outdated
/// Furthermore, `usize` and `isize` will have the same size. | ||
/// | ||
/// The types `*T`, `&T`, and `Box<T>` have the same size | ||
/// and `Option`s of `&T` and `Box<T>` have the same size as the pointer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this a little hard to parse. A skeptical reader would read this as:
Options &T
\ /
of Box<T>
\ /
and the pointer
\ /
have the same size as
instead of
&T Box<T>
\ /
Options and
\ /
of the pointer
\ /
have the same size as
because that sounds more plausible to someone skeptical of zero overhead abstractions.
Is there a way to word this that shows the full types Option<&T>
and Option<Box<T>>
in the sentence?
Maybe something straightforward like:
The types *T, &T, Box<T>, Option<&T>, and Option<Box<T>> all have the same size. If T is Sized, all of those have the same size as usize.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was sort of floundering with that section. That reads a lot better.
src/libcore/mem.rs
Outdated
/// | ||
/// The following table gives the size for primitives. | ||
/// | ||
/// Type | size_of<Type>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_of::<Type>()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As durka wrote, this would be clearer as size_of::<Type>()
instead of size_of<Type>()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also I believe these need to be in backticks or escaped with a backslash. Otherwise markdown will treat it as a <Type> HTML tag.
`Type` | `size_of::<Type>()`
Type | size_of::\<Type>()
src/libcore/mem.rs
Outdated
/// | ||
/// Furthermore, `usize` and `isize` will have the same size. | ||
/// | ||
/// The types `*T`, `&T`, and `Box<T>` have the same size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be the first place we say *T
instead of *const T
/*mut T
? Likewise, everything here about &T
applies to &mut T
.
src/libcore/mem.rs
Outdated
/// | ||
/// | ||
/// // Pointer size equality | ||
/// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<*i32>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error: expected mut or const in raw pointer type (use `*mut T` or `*const T` as appropriate)
src/libcore/mem.rs
Outdated
/// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<*i32>()); | ||
/// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Box<i32>()); | ||
/// assert_eq!(mem::size_of::<&i32>(), mem::size_of::<Option<&i32>>()); | ||
/// assert_eq!(mem::size_of::<Box<i32>>(), mem::size_of::<Option<Box<32>>>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo Box<32>
One more type we guarantee is |
src/libcore/mem.rs
Outdated
/// // Some arrays | ||
/// assert_eq!(8, mem::size_of::<[i32; 2]>()); | ||
/// assert_eq!(12, mem::size_of::<[i32, 3]>()); | ||
/// assert_eq!(0, mem::size_of::<[i32, 0]>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
;
to separate i32 from the length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I was running the doctests; I actually wasn't.
All nits should be fixed. I don't have dtolnay's thing here. I'm wondering if the null pointer optimization for Option-shaped enums is actually stable, and if so, we could just say that, and then point out what the common nullable pointer types are, including I'm avoiding talking about I can also squash all the commits into one if you want. |
I like what you have here; the Details about enum size is probably mixed up in #27730 & rust-lang/rfcs#1230, and fine to leave for later. |
src/libcore/mem.rs
Outdated
/// items of the same type, including alignment padding. | ||
/// More specifically, this is the offset in bytes between successive elements | ||
/// in an array with that item type including alignment padding. Thus, for any | ||
/// type `T` and length `n`, [T; n] will have a size of `n * size_of::<T>()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[T; n]
should be in backticks.
src/libcore/mem.rs
Outdated
/// | ||
/// The following table gives the size for primitives. | ||
/// | ||
/// Type | size_of<Type>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As durka wrote, this would be clearer as size_of::<Type>()
instead of size_of<Type>()
.
src/libcore/mem.rs
Outdated
/// f64 | 8 | ||
/// char | 4 | ||
/// | ||
/// Furthermore, `usize` and `isize` will have the same size. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest of this documentation is in present tense so "will have" makes it sound like this part is not implemented yet. Plain "have" should be sufficient I think.
src/libcore/mem.rs
Outdated
/// assert_eq!(4, mem::size_of::<i32>()); | ||
/// assert_eq!(8, mem::size_of::<f64)()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As kennytm wrote, there is a typo after f64.
src/libcore/mem.rs
Outdated
/// | ||
/// // Some arrays | ||
/// assert_eq!(8, mem::size_of::<[i32; 2]>()); | ||
/// assert_eq!(12, mem::size_of::<[i32, 3]>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should say [i32; 3]
instead of [i32, 3]
.
r=me after the last few typos are addressed. I agree we don't need to include |
Finally figured out how to get rustdoc tests to compile for this. Items in libstd from the libcore facade don't actually get tested from Also rebased everything into a single commit to avoid polluting the commit log with typo fixing and whatnot. |
@bors r+ |
📌 Commit 548686f has been approved by |
@bors rollup |
Expand size_of docs This PR does 3 things. 1. Adds a description of what pointer size means to the primitive pages for usize and isize. 2. Says the general size of things is not stable from compiler to compiler. 3. Adds a table of sizes of things that we do guarantee. As this is the first table in the libstd docs, I've included a picture of how that looks. ![](https://i.imgur.com/YZ6IChH.png?1)
Expand size_of docs This PR does 3 things. 1. Adds a description of what pointer size means to the primitive pages for usize and isize. 2. Says the general size of things is not stable from compiler to compiler. 3. Adds a table of sizes of things that we do guarantee. As this is the first table in the libstd docs, I've included a picture of how that looks. ![](https://i.imgur.com/YZ6IChH.png?1)
This PR does 3 things.