Skip to content

Commit 4b9fbfb

Browse files
authored
Rollup merge of rust-lang#65144 - clarfon:moo, r=sfackler
Add Cow::is_borrowed and Cow::is_owned Implements rust-lang#65143.
2 parents 426c6cf + eeb549b commit 4b9fbfb

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/liballoc/borrow.rs

+41
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,47 @@ impl<B: ?Sized + ToOwned> Clone for Cow<'_, B> {
207207
}
208208

209209
impl<B: ?Sized + ToOwned> Cow<'_, B> {
210+
/// Returns true if the data is borrowed, i.e. if `to_mut` would require additional work.
211+
///
212+
/// # Examples
213+
///
214+
/// ```
215+
/// #![feature(cow_is_borrowed)]
216+
/// use std::borrow::Cow;
217+
///
218+
/// let cow = Cow::Borrowed("moo");
219+
/// assert!(cow.is_borrowed());
220+
///
221+
/// let bull: Cow<'_, str> = Cow::Owned("...moo?".to_string());
222+
/// assert!(!bull.is_borrowed());
223+
/// ```
224+
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
225+
pub fn is_borrowed(&self) -> bool {
226+
match *self {
227+
Borrowed(_) => true,
228+
Owned(_) => false,
229+
}
230+
}
231+
232+
/// Returns true if the data is owned, i.e. if `to_mut` would be a no-op.
233+
///
234+
/// # Examples
235+
///
236+
/// ```
237+
/// #![feature(cow_is_borrowed)]
238+
/// use std::borrow::Cow;
239+
///
240+
/// let cow: Cow<'_, str> = Cow::Owned("moo".to_string());
241+
/// assert!(cow.is_owned());
242+
///
243+
/// let bull = Cow::Borrowed("...moo?");
244+
/// assert!(!bull.is_owned());
245+
/// ```
246+
#[unstable(feature = "cow_is_borrowed", issue = "65143")]
247+
pub fn is_owned(&self) -> bool {
248+
!self.is_borrowed()
249+
}
250+
210251
/// Acquires a mutable reference to the owned form of the data.
211252
///
212253
/// Clones the data if it is not already owned.

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#![feature(const_generic_impls_guard)]
8686
#![feature(const_generics)]
8787
#![feature(const_in_array_repeat_expressions)]
88+
#![feature(cow_is_borrowed)]
8889
#![feature(dispatch_from_dyn)]
8990
#![feature(core_intrinsics)]
9091
#![feature(container_error_extra)]

0 commit comments

Comments
 (0)