-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
allocator: refactor for stabilisation #157428
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
base: main
Are you sure you want to change the base?
Changes from 19 commits
354678d
ecfda4e
565c994
d82182c
a3db8cd
0fca0d1
d4cb394
24f496f
4868441
ad88f59
5af9c06
c172f93
dffda9e
aa9abd6
59219ba
4805e24
ffdb858
0c2cf06
c2908f6
c4ac24b
1740dfe
080f7eb
8a83c61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -206,10 +206,12 @@ use core::task::{Context, Poll}; | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[cfg(not(no_global_oom_handling))] | ||||||||||||||||||||||||
| use crate::alloc::handle_alloc_error; | ||||||||||||||||||||||||
| use crate::alloc::{AllocError, Allocator, Global, Layout}; | ||||||||||||||||||||||||
| use crate::alloc::{ | ||||||||||||||||||||||||
| AllocError, Allocator, AllocatorClone, AllocatorEq, Global, Layout, StaticAllocator, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| use crate::raw_vec::RawVec; | ||||||||||||||||||||||||
| #[cfg(not(no_global_oom_handling))] | ||||||||||||||||||||||||
| use crate::str::from_boxed_utf8_unchecked; | ||||||||||||||||||||||||
| use crate::str::from_boxed_utf8_unchecked_in; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Conversion related impls for `Box<_>` (`From`, `downcast`, etc) | ||||||||||||||||||||||||
| mod convert; | ||||||||||||||||||||||||
|
|
@@ -712,7 +714,7 @@ impl<T, A: Allocator> Box<T, A> { | |||||||||||||||||||||||
| #[inline(always)] | ||||||||||||||||||||||||
| pub fn pin_in(x: T, alloc: A) -> Pin<Self> | ||||||||||||||||||||||||
| where | ||||||||||||||||||||||||
| A: 'static + Allocator, | ||||||||||||||||||||||||
| A: StaticAllocator, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| Self::into_pin(Self::new_in(x, alloc)) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -1979,7 +1981,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> { | |||||||||||||||||||||||
| #[stable(feature = "box_into_pin", since = "1.63.0")] | ||||||||||||||||||||||||
| pub fn into_pin(boxed: Self) -> Pin<Self> | ||||||||||||||||||||||||
| where | ||||||||||||||||||||||||
| A: 'static, | ||||||||||||||||||||||||
| A: StaticAllocator, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| // It's not possible to move or replace the insides of a `Pin<Box<T>>` | ||||||||||||||||||||||||
| // when `T: !Unpin`, so it's safe to pin it directly without any | ||||||||||||||||||||||||
|
|
@@ -2153,11 +2155,10 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[cfg(not(no_global_oom_handling))] | ||||||||||||||||||||||||
| #[stable(feature = "box_slice_clone", since = "1.3.0")] | ||||||||||||||||||||||||
| impl Clone for Box<str> { | ||||||||||||||||||||||||
| impl<A: Allocator + Clone> Clone for Box<str, A> { | ||||||||||||||||||||||||
| fn clone(&self) -> Self { | ||||||||||||||||||||||||
| // this makes a copy of the data | ||||||||||||||||||||||||
| let buf: Box<[u8]> = self.as_bytes().into(); | ||||||||||||||||||||||||
| unsafe { from_boxed_utf8_unchecked(buf) } | ||||||||||||||||||||||||
| let buf = Box::clone_from_ref_in(self.as_bytes(), self.1.clone()); | ||||||||||||||||||||||||
| unsafe { from_boxed_utf8_unchecked_in(buf) } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -2369,7 +2370,7 @@ impl<Args: Tuple, F: AsyncFn<Args> + ?Sized, A: Allocator> AsyncFn<Args> for Box | |||||||||||||||||||||||
| impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {} | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[unstable(feature = "pin_coerce_unsized_trait", issue = "150112")] | ||||||||||||||||||||||||
| unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Box<T, A> {} | ||||||||||||||||||||||||
| unsafe impl<T: ?Sized, A: StaticAllocator> PinCoerceUnsized for Box<T, A> {} | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // It is quite crucial that we only allow the `Global` allocator here. | ||||||||||||||||||||||||
| // Handling arbitrary custom allocators (which can affect the `Box` layout heavily!) | ||||||||||||||||||||||||
|
|
@@ -2529,3 +2530,21 @@ unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> { | |||||||||||||||||||||||
| unsafe { (**self).shrink(ptr, old_layout, new_layout) } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[unstable(feature = "allocator_api", issue = "32838")] | ||||||||||||||||||||||||
| unsafe impl<T, A> AllocatorClone for Box<T, A> | ||||||||||||||||||||||||
| where | ||||||||||||||||||||||||
| T: AllocatorClone, | ||||||||||||||||||||||||
| // Otherwise, the clone impl *here* may unwind. | ||||||||||||||||||||||||
| A: AllocatorClone, | ||||||||||||||||||||||||
| Box<T, A>: Clone, | ||||||||||||||||||||||||
|
Comment on lines
+2538
to
+2543
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oop yeah ur right
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we could relax this a little more to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i tried to, but that resulted in an error ^^ trait solver is not smart enough to realise that the previous bounds are enough to guarantee that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah 😅. Is it intended that we are allowed to write
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds really close to stuff being unsound already...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, I'll make an issue, I already have the code ready ^^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might be able to use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[unstable(feature = "allocator_api", issue = "32838")] | ||||||||||||||||||||||||
| unsafe impl<T, A> AllocatorEq for Box<T, A> | ||||||||||||||||||||||||
| where | ||||||||||||||||||||||||
| T: AllocatorEq + ?Sized, | ||||||||||||||||||||||||
| A: Allocator, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -896,6 +896,22 @@ pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> { | |
| unsafe { Box::from_raw(Box::into_raw(v) as *mut str) } | ||
| } | ||
|
|
||
| /// Converts a boxed slice of bytes to a boxed string slice without checking | ||
| /// that the string contains valid UTF-8 generically over the box's allocator. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// * The provided bytes must contain a valid UTF-8 sequence. | ||
| #[unstable(feature = "allocator_api", issue = "32838")] | ||
| #[must_use] | ||
| #[inline] | ||
| pub unsafe fn from_boxed_utf8_unchecked_in<A: crate::alloc::Allocator>( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to nitpick too strongly but generally I would expect the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't mind changing the name or even making this private. i just needed the method for that one conversion /shrug |
||
| v: Box<[u8], A>, | ||
| ) -> Box<str, A> { | ||
| let (ptr, alloc) = Box::into_raw_with_allocator(v); | ||
| unsafe { Box::from_raw_in(ptr as *mut str, alloc) } | ||
| } | ||
|
|
||
| /// Converts leading ascii bytes in `s` by calling the `convert` function. | ||
| /// | ||
| /// For better average performance, this happens in chunks of `2*size_of::<usize>()`. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.