-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add a function to turn Box<T> into Box<[T]> #71421
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
I think it is better to have an issue about this, and also maybe add a test |
25175e6
to
4693236
Compare
Added tests, is this what you were looking for? (I'm not sure why every error appears twice) |
4693236
to
29f7729
Compare
Those are all about the alignment of pointer-like values themselves, regardless of |
That makes sense. the usage of |
src/liballoc/boxed.rs
Outdated
@@ -816,6 +827,18 @@ impl From<Box<str>> for Box<[u8]> { | |||
} | |||
} | |||
|
|||
#[unstable(feature = "box_into_boxed_slice", issue = "none")] | |||
impl<T> From<Box<T>> for Box<[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.
AFAIK, trait impls are effectively insta-stable -- they can't really be unstable unless the trait and/or types involved are unstable.
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.
ha, so I'll remove this from, and it can be added back if/when this is stable
29f7729
to
ba79691
Compare
What's the use case for this? |
I have a function that returns https://docs.rs/syn/1.0.18/syn/struct.TypeParen.html EDIT, basically something along these lines: fn extract_tupled_types(attr: Attribute) -> Result<Vec<Type>> {
let ty: Type = syn::parse2(attr.tts)?;
Ok(match ty {
Type::Paren(paren) => vec![*paren.elem],
Type::Tuple(tuple) => tuple.elems.into_iter().collect(),
_ => vec![ty],
}) |
src/liballoc/boxed.rs
Outdated
/// | ||
/// This conversion does not allocate on the heap and happens in place. | ||
/// | ||
/// This is also available via [`From`]. |
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.
Is this true?
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.
Ops, thanks. I just removed the From impl because it's insta-stable
Seems plausible to me. Can you make a tracking issue and update the stability annotation? |
👍 Thanks! |
ba79691
to
0228ca0
Compare
@bors r+ rollup |
📌 Commit 0228ca0 has been approved by |
Rollup of 5 pull requests Successful merges: - rust-lang#71421 (Add a function to turn Box<T> into Box<[T]>) - rust-lang#71537 (Remove support for self-opening) - rust-lang#71551 (Minor refactoring around IndexVec usage in generator transformation) - rust-lang#71569 ([miri] Throw UB if target size and data size don't match) - rust-lang#71576 (check that `AsRef` and `AsMut` are inlined) Failed merges: - rust-lang#71558 (Cleanup and document `-Z tls-model` ) r? @ghost
/// | ||
#[unstable(feature = "box_into_boxed_slice", issue = "71582")] | ||
pub fn into_boxed_slice(boxed: Box<T>) -> Box<[T]> { | ||
// *mut T and *mut [T; 1] have the same size and alignment |
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 comment doesn't match the code. *mut T
≠ *mut [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.
That comment is justifying the cast to an array of length 1. The slice comes after.
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.
What justifies the subsequent cast?
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.
That's just unsizing, but we could leave that out and let the Box<[T; 1]>
implicitly coerce unsized in the return.
Hi,
I think this is very useful, as currently it's not possible in safe rust to do this without re-allocating.
an alternative implementation of the same function can be:
The only thing that makes me a little uncomfortable is this line :
from https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html
But then I see:
from https://rust-lang.github.io/unsafe-code-guidelines/layout/pointers.html#representation
So I do believe this is valid(FWIW it also passes in miri https://play.rust-lang.org/?gist=c002b99364ee6b29862aeb3565a91c19)