From 931eb4c4d5169ebff249387f2049295eb7925c79 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Mon, 6 Aug 2018 17:33:32 +0300 Subject: [PATCH 1/3] Add one more example for Cow that shows how to keep Cow in a struct --- src/liballoc/borrow.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs index c6741ddb822d5..dd4f958804dee 100644 --- a/src/liballoc/borrow.rs +++ b/src/liballoc/borrow.rs @@ -141,6 +141,40 @@ impl ToOwned for T /// let mut input = Cow::from(vec![-1, 0, 1]); /// abs_all(&mut input); /// ``` +/// +/// ``` +/// use std::borrow::{Cow, ToOwned}; +/// +/// struct Items<'a, X: 'a> where [X]: ToOwned> { +/// values: Cow<'a, [X]>, +/// } +/// +/// impl<'a, X: Clone + 'a> Items<'a, X> where [X]: ToOwned> { +/// fn new(v: Cow<'a, [X]>) -> Self { +/// Items { values: v } +/// } +/// } +/// +/// // Creates a container from borrowed values of a slice +/// let readonly = [1, 2]; +/// let borrowed = Items::new((&readonly[..]).into()); +/// match borrowed { +/// Items { values: Cow::Borrowed(b) } => println!("borrowed {:?}", b), +/// _ => panic!("expect borrowed value"), +/// } +/// +/// let mut clone_on_write = borrowed; +/// // Mutates the data from slice into owned vec and pushes a new value on top +/// clone_on_write.values.to_mut().push(3); +/// println!("clone_on_write = {:?}", clone_on_write.values); +/// +/// // The data was mutated. Let check it out. +/// match clone_on_write { +/// Items { values: Cow::Owned(_) } => println!("clone_on_write contains owned data"), +/// _ => panic!("expect owned data"), +/// } +/// ``` + #[stable(feature = "rust1", since = "1.0.0")] pub enum Cow<'a, B: ?Sized + 'a> where B: ToOwned From 9a3a12eaa37b46f4eedd0c93fa61f35e2d748ee8 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Wed, 15 Aug 2018 14:00:59 +0300 Subject: [PATCH 2/3] Fix review notes --- src/liballoc/borrow.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs index dd4f958804dee..95980735f7e14 100644 --- a/src/liballoc/borrow.rs +++ b/src/liballoc/borrow.rs @@ -142,6 +142,7 @@ impl ToOwned for T /// abs_all(&mut input); /// ``` /// +/// Another example showing how to keep `Cow` in a struct: /// ``` /// use std::borrow::{Cow, ToOwned}; /// @@ -174,7 +175,6 @@ impl ToOwned for T /// _ => panic!("expect owned data"), /// } /// ``` - #[stable(feature = "rust1", since = "1.0.0")] pub enum Cow<'a, B: ?Sized + 'a> where B: ToOwned From 5bfb7850782c440f27e0a5b64157aed9e04c5cc6 Mon Sep 17 00:00:00 2001 From: Roman Proskuryakov Date: Wed, 15 Aug 2018 22:06:35 +0300 Subject: [PATCH 3/3] Review fix --- src/liballoc/borrow.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs index 95980735f7e14..5ae5339138fbe 100644 --- a/src/liballoc/borrow.rs +++ b/src/liballoc/borrow.rs @@ -143,6 +143,7 @@ impl ToOwned for T /// ``` /// /// Another example showing how to keep `Cow` in a struct: +/// /// ``` /// use std::borrow::{Cow, ToOwned}; ///