Skip to content

Commit e6cb4de

Browse files
Fixing A and COOP_PREFERRED generics. WIP..
1 parent 701135d commit e6cb4de

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

library/alloc/src/str.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,10 @@ impl str {
505505
#[rustc_allow_incoherent_impl]
506506
#[must_use = "`self` will be dropped if the result is not used"]
507507
#[inline]
508-
pub fn into_string(self: Box<str>) -> String {
508+
pub fn into_string<const COOP_PREFERRED: bool>(self: Box<str>) -> String<COOP_PREFERRED>
509+
where
510+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
511+
{
509512
let slice = Box::<[u8]>::from(self);
510513
unsafe { String::from_utf8_unchecked(slice.into_vec()) }
511514
}

library/alloc/src/string.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,13 +1480,19 @@ where
14801480
where
14811481
F: FnMut(char) -> bool,
14821482
{
1483-
struct SetLenOnDrop<'a> {
1484-
s: &'a mut String,
1483+
struct SetLenOnDrop<'a, const LOCAL_COOP_PREFERRED: bool>
1484+
where
1485+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(LOCAL_COOP_PREFERRED)]:,
1486+
{
1487+
s: &'a mut String<LOCAL_COOP_PREFERRED>,
14851488
idx: usize,
14861489
del_bytes: usize,
14871490
}
14881491

1489-
impl<'a> Drop for SetLenOnDrop<'a> {
1492+
impl<'a, const LOCAL_COOP_PREFERRED: bool> Drop for SetLenOnDrop<'a, LOCAL_COOP_PREFERRED>
1493+
where
1494+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(LOCAL_COOP_PREFERRED)]:,
1495+
{
14901496
fn drop(&mut self) {
14911497
let new_len = self.idx - self.del_bytes;
14921498
debug_assert!(new_len <= self.s.len());
@@ -1783,7 +1789,7 @@ where
17831789
/// assert_eq!(s, "");
17841790
/// ```
17851791
#[stable(feature = "drain", since = "1.6.0")]
1786-
pub fn drain<R>(&mut self, range: R) -> Drain<'_>
1792+
pub fn drain<R>(&mut self, range: R) -> Drain<'_, COOP_PREFERRED>
17871793
where
17881794
R: RangeBounds<usize>,
17891795
{
@@ -2216,16 +2222,16 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(CO
22162222

22172223
#[cfg(not(no_global_oom_handling))]
22182224
#[stable(feature = "extend_string", since = "1.4.0")]
2219-
impl<const COOP_PREFERRED: bool> Extend<String> for String<COOP_PREFERRED>
2225+
impl<const COOP_PREFERRED: bool> Extend<String<COOP_PREFERRED>> for String<COOP_PREFERRED>
22202226
where
22212227
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
22222228
{
2223-
fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
2229+
fn extend<I: IntoIterator<Item = String<COOP_PREFERRED>>>(&mut self, iter: I) {
22242230
iter.into_iter().for_each(move |s| self.push_str(&s));
22252231
}
22262232

22272233
#[inline]
2228-
fn extend_one(&mut self, s: String) {
2234+
fn extend_one(&mut self, s: String<COOP_PREFERRED>) {
22292235
self.push_str(&s);
22302236
}
22312237
}
@@ -3109,6 +3115,7 @@ where
31093115
/// [`drain`]: String::drain
31103116
#[stable(feature = "drain", since = "1.6.0")]
31113117
#[allow(unused_braces)]
3118+
// @FIXME Do we need to use DEFAULT_COOP_PREFERRED here?
31123119
pub struct Drain<'a, const COOP_PREFERRED: bool = {DEFAULT_COOP_PREFERRED!()}>
31133120
where
31143121
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,

library/alloc/src/vec/into_iter.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,17 @@ where
131131
// struct and then overwriting &mut self.
132132
// this creates less assembly
133133
self.cap = 0;
134-
self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) };
134+
self.buf = unsafe {
135+
// @FIXME The below if COOP_PREFERRED {..} else {..}
136+
// branching exists, because the following fails. Otherwise we'd have a snowball effect of wide spread of where...Global...
137+
//
138+
// NonNull::new_unchecked(RawVec::<T, Global, COOP_PREFERRED>::NEW.ptr())
139+
if COOP_PREFERRED {
140+
NonNull::new_unchecked(RawVec::<T, Global, true>::NEW.ptr())
141+
} else {
142+
NonNull::new_unchecked(RawVec::<T, Global, false>::NEW.ptr())
143+
}
144+
};
135145
self.ptr = self.buf.as_ptr();
136146
self.end = self.buf.as_ptr();
137147

library/alloc/src/vec/splice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ where
115115
}
116116

117117
/// Private helper methods for `Splice::drop`
118-
impl<T, A: Allocator> Drain<'_, T, A>
118+
impl<T, A: Allocator, const COOP_PREFERRED: bool> Drain<'_, T, A, COOP_PREFERRED>
119119
where
120-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(true)]:,
120+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
121121
{
122122
/// The range from `self.vec.len` to `self.tail_start` contains elements
123123
/// that have been moved out.

0 commit comments

Comments
 (0)