Skip to content

Commit 87499b8

Browse files
Adding COOP_PREFERRED to Cow, ToOwned..
1 parent ab2b964 commit 87499b8

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

library/alloc/src/borrow.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ use crate::string::String;
1818
use Cow::*;
1919

2020
#[stable(feature = "rust1", since = "1.0.0")]
21-
impl<'a, B: ?Sized> Borrow<B> for Cow<'a, B>
21+
impl<'a, B: ?Sized, const COOP_PREFERRED: bool> Borrow<B> for Cow<'a, B, COOP_PREFERRED>
2222
where
23-
B: ToOwned,
23+
B: ToOwned<COOP_PREFERRED>,
24+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
25+
2426
{
2527
fn borrow(&self) -> &B {
2628
&**self
@@ -83,9 +85,10 @@ where
8385
}
8486

8587
#[stable(feature = "rust1", since = "1.0.0")]
86-
impl<T> ToOwned for T
88+
impl<T, const COOP_PREFERRED: bool> ToOwned<COOP_PREFERRED> for T
8789
where
8890
T: Clone,
91+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
8992
{
9093
type Owned = T;
9194
fn to_owned(&self) -> T {
@@ -180,21 +183,26 @@ where
180183
/// ```
181184
#[stable(feature = "rust1", since = "1.0.0")]
182185
#[cfg_attr(not(test), rustc_diagnostic_item = "Cow")]
183-
pub enum Cow<'a, B: ?Sized + 'a>
186+
#[allow(unused_braces)]
187+
pub enum Cow<'a, B: ?Sized + 'a, const COOP_PREFERRED: bool = { DEFAULT_COOP_PREFERRED!() }>
184188
where
185-
B: ToOwned,
189+
B: ToOwned<COOP_PREFERRED>,
190+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
186191
{
187192
/// Borrowed data.
188193
#[stable(feature = "rust1", since = "1.0.0")]
189194
Borrowed(#[stable(feature = "rust1", since = "1.0.0")] &'a B),
190195

191196
/// Owned data.
192197
#[stable(feature = "rust1", since = "1.0.0")]
193-
Owned(#[stable(feature = "rust1", since = "1.0.0")] <B as ToOwned>::Owned),
198+
Owned(#[stable(feature = "rust1", since = "1.0.0")] <B as ToOwned<COOP_PREFERRED>>::Owned),
194199
}
195200

196201
#[stable(feature = "rust1", since = "1.0.0")]
197-
impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
202+
impl<B: ?Sized + ToOwned<COOP_PREFERRED>, const COOP_PREFERRED: bool> Clone for Cow<'_, B, COOP_PREFERRED>
203+
where
204+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
205+
{
198206
fn clone(&self) -> Self {
199207
match *self {
200208
Borrowed(b) => Borrowed(b),
@@ -213,7 +221,10 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
213221
}
214222
}
215223

216-
impl<B: ?Sized + ToOwned> Cow<'_, B> {
224+
impl<B: ?Sized + ToOwned<COOP_PREFERRED>, const COOP_PREFERRED: bool> Cow<'_, B, COOP_PREFERRED>
225+
where
226+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
227+
{
217228
/// Returns true if the data is borrowed, i.e. if `to_mut` would require additional work.
218229
///
219230
/// # Examples
@@ -275,7 +286,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
275286
/// );
276287
/// ```
277288
#[stable(feature = "rust1", since = "1.0.0")]
278-
pub fn to_mut(&mut self) -> &mut <B as ToOwned>::Owned {
289+
pub fn to_mut(&mut self) -> &mut <B as ToOwned<COOP_PREFERRED>>::Owned {
279290
match *self {
280291
Borrowed(borrowed) => {
281292
*self = Owned(borrowed.to_owned());
@@ -323,7 +334,7 @@ impl<B: ?Sized + ToOwned> Cow<'_, B> {
323334
/// );
324335
/// ```
325336
#[stable(feature = "rust1", since = "1.0.0")]
326-
pub fn into_owned(self) -> <B as ToOwned>::Owned {
337+
pub fn into_owned(self) -> <B as ToOwned<COOP_PREFERRED>>::Owned {
327338
match self {
328339
Borrowed(borrowed) => borrowed.to_owned(),
329340
Owned(owned) => owned,
@@ -385,9 +396,10 @@ where
385396
}
386397

387398
#[stable(feature = "rust1", since = "1.0.0")]
388-
impl<B: ?Sized> fmt::Debug for Cow<'_, B>
399+
impl<B: ?Sized, const COOP_PREFERRED: bool> fmt::Debug for Cow<'_, B, COOP_PREFERRED>
389400
where
390-
B: fmt::Debug + ToOwned<Owned: fmt::Debug>,
401+
B: fmt::Debug + ToOwned<COOP_PREFERRED, Owned: fmt::Debug>,
402+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
391403
{
392404
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
393405
match *self {
@@ -398,9 +410,10 @@ where
398410
}
399411

400412
#[stable(feature = "rust1", since = "1.0.0")]
401-
impl<B: ?Sized> fmt::Display for Cow<'_, B>
413+
impl<B: ?Sized, const COOP_PREFERRED: bool> fmt::Display for Cow<'_, B, COOP_PREFERRED>
402414
where
403-
B: fmt::Display + ToOwned<Owned: fmt::Display>,
415+
B: fmt::Display + ToOwned<COOP_PREFERRED, Owned: fmt::Display>,
416+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
404417
{
405418
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
406419
match *self {
@@ -411,13 +424,14 @@ where
411424
}
412425

413426
#[stable(feature = "default", since = "1.11.0")]
414-
impl<B: ?Sized> Default for Cow<'_, B>
427+
impl<B: ?Sized, const COOP_PREFERRED: bool> Default for Cow<'_, B, COOP_PREFERRED>
415428
where
416-
B: ToOwned<Owned: Default>,
429+
B: ToOwned<COOP_PREFERRED, Owned: Default>,
430+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
417431
{
418432
/// Creates an owned Cow<'a, B> with the default value for the contained owned value.
419433
fn default() -> Self {
420-
Owned(<B as ToOwned>::Owned::default())
434+
Owned(<B as ToOwned<COOP_PREFERRED>>::Owned::default())
421435
}
422436
}
423437

library/alloc/src/ffi/c_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ impl CStr {
11021102
#[must_use = "this returns the result of the operation, \
11031103
without modifying the original"]
11041104
#[stable(feature = "cstr_to_str", since = "1.4.0")]
1105-
pub fn to_string_lossy(&self) -> Cow<'_, str> {
1105+
pub fn to_string_lossy(&self) -> Cow<'_, str, false> {
11061106
// false = no need for co-alloc metadata, since it would get lost once converted to the slice.
11071107
String::<false>::from_utf8_lossy(self.to_bytes())
11081108
}

library/alloc/src/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ where
215215
type Owned = String<COOP_PREFERRED>;
216216
#[inline]
217217
fn to_owned(&self) -> String<COOP_PREFERRED> {
218-
unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
218+
unsafe { String::<COOP_PREFERRED>::from_utf8_unchecked(self.as_bytes().to_owned()) }
219219
}
220220

221221
fn clone_into(&self, target: &mut String<COOP_PREFERRED>) {
222222
let mut b = mem::take(target).into_bytes();
223223
self.as_bytes().clone_into(&mut b);
224-
*target = unsafe { String::from_utf8_unchecked(b) }
224+
*target = unsafe { String::<COOP_PREFERRED>::from_utf8_unchecked(b) }
225225
}
226226
}
227227

library/alloc/src/string.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ where
646646
#[must_use]
647647
#[cfg(not(no_global_oom_handling))]
648648
#[stable(feature = "rust1", since = "1.0.0")]
649-
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
649+
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> {
650650
let mut iter = Utf8Chunks::new(v);
651651

652652
let first_valid = if let Some(chunk) = iter.next() {
@@ -662,7 +662,7 @@ where
662662

663663
const REPLACEMENT: &str = "\u{FFFD}";
664664

665-
let mut res = String::<false>::with_capacity(v.len());
665+
let mut res = String::<COOP_PREFERRED>::with_capacity(v.len());
666666
res.push_str(first_valid);
667667
res.push_str(REPLACEMENT);
668668

@@ -2132,7 +2132,7 @@ impl<'a, const COOP_PREFERRED: bool> FromIterator<Cow<'a, str>> for String<COOP_
21322132
where
21332133
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
21342134
{
2135-
fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String<COOP_PREFERRED> {
2135+
fn from_iter<I: IntoIterator<Item = Cow<'a, str, COOP_PREFERRED>>>(iter: I) -> String<COOP_PREFERRED> {
21362136
let mut iterator = iter.into_iter();
21372137

21382138
// Because we're iterating over CoWs, we can (potentially) avoid at least
@@ -2960,7 +2960,7 @@ where
29602960
/// let owned: String = String::from(cow);
29612961
/// assert_eq!(&owned[..], "eggplant");
29622962
/// ```
2963-
fn from(s: Cow<'a, str>) -> String<COOP_PREFERRED> {
2963+
fn from(s: Cow<'a, str, COOP_PREFERRED>) -> String<COOP_PREFERRED> {
29642964
s.into_owned()
29652965
}
29662966
}
@@ -3007,7 +3007,7 @@ where
30073007
///
30083008
/// [`Owned`]: crate::borrow::Cow::Owned "borrow::Cow::Owned"
30093009
#[inline]
3010-
fn from(s: String<COOP_PREFERRED>) -> Cow<'a, str> {
3010+
fn from(s: String<COOP_PREFERRED>) -> Cow<'a, str, COOP_PREFERRED> {
30113011
Cow::Owned(s)
30123012
}
30133013
}
@@ -3032,7 +3032,7 @@ where
30323032
///
30333033
/// [`Borrowed`]: crate::borrow::Cow::Borrowed "borrow::Cow::Borrowed"
30343034
#[inline]
3035-
fn from(s: &'a String<COOP_PREFERRED>) -> Cow<'a, str> {
3035+
fn from(s: &'a String<COOP_PREFERRED>) -> Cow<'a, str, COOP_PREFERRED> {
30363036
Cow::Borrowed(s.as_str())
30373037
}
30383038
}
@@ -3059,7 +3059,7 @@ impl<'a, const COOP_PREFERRED: bool> FromIterator<String<COOP_PREFERRED>> for Co
30593059
where
30603060
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
30613061
{
3062-
fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>(it: I) -> Cow<'a, str> {
3062+
fn from_iter<I: IntoIterator<Item = String<COOP_PREFERRED>>>(it: I) -> Cow<'a, str, COOP_PREFERRED> {
30633063
Cow::Owned(FromIterator::from_iter(it))
30643064
}
30653065
}

0 commit comments

Comments
 (0)