From 236b7487d525359440815a425cba97fa36903afc Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Sun, 23 Jul 2017 14:18:34 +0200 Subject: [PATCH 1/6] Add simple docs example for struct Cell --- src/libcore/cell.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 35744f3f16b39..7e12d5466c2bf 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -187,6 +187,29 @@ use ops::{Deref, DerefMut, CoerceUnsized}; use ptr; /// A mutable memory location. +/// +/// ``` +/// use std::cell::Cell; +/// +/// struct SomeStruct { +/// regular_field: u8, +/// special_field: Cell, +/// } +/// +/// let my_struct = SomeStruct { +/// regular_field: 0, +/// special_field: Cell::new(1), +/// }; +/// +/// let new_value = 100; +/// +/// // ERROR, because my_struct is immutable +/// // immutable.regular_field = new_value; +/// +/// // WORKS, special_field is mutable because it is Cell +/// immutable.special_field.set(new_value); +/// assert_eq!(immutable.special_field.get(), new_value); +/// ``` /// /// See the [module-level documentation](index.html) for more. #[stable(feature = "rust1", since = "1.0.0")] From bb65d3256841e1a7d267a3177a9147fd83857727 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 16:23:26 +0200 Subject: [PATCH 2/6] add prose --- src/libcore/cell.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 7e12d5466c2bf..acff77004ee40 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -188,6 +188,11 @@ use ptr; /// A mutable memory location. /// +/// # Example +/// +/// Here you can see how using `Cell` allows to use muttable field inside +/// immutable struct (which is also called "interior mutability"). +/// /// ``` /// use std::cell::Cell; /// @@ -206,7 +211,7 @@ use ptr; /// // ERROR, because my_struct is immutable /// // immutable.regular_field = new_value; /// -/// // WORKS, special_field is mutable because it is Cell +/// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell /// immutable.special_field.set(new_value); /// assert_eq!(immutable.special_field.get(), new_value); /// ``` From 3c535952bc7df52b8b9becae26511fb6ccdab7b1 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 18:01:50 +0200 Subject: [PATCH 3/6] review fixes --- src/libcore/cell.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index acff77004ee40..804d95f12c42d 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -188,10 +188,10 @@ use ptr; /// A mutable memory location. /// -/// # Example +/// # Examples /// -/// Here you can see how using `Cell` allows to use muttable field inside -/// immutable struct (which is also called "interior mutability"). +/// Here you can see how using `Cell` allows to use mutable field inside +/// immutable struct (which is also called 'interior mutability'). /// /// ``` /// use std::cell::Cell; From 82860753463314e6a1b94f1f97d4d9c4effc0742 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 18:07:51 +0200 Subject: [PATCH 4/6] ci fix? --- src/libcore/cell.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 804d95f12c42d..1610e89a82d73 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -187,12 +187,9 @@ use ops::{Deref, DerefMut, CoerceUnsized}; use ptr; /// A mutable memory location. -/// /// # Examples -/// /// Here you can see how using `Cell` allows to use mutable field inside /// immutable struct (which is also called 'interior mutability'). -/// /// ``` /// use std::cell::Cell; /// @@ -207,10 +204,8 @@ use ptr; /// }; /// /// let new_value = 100; -/// /// // ERROR, because my_struct is immutable /// // immutable.regular_field = new_value; -/// /// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell /// immutable.special_field.set(new_value); /// assert_eq!(immutable.special_field.get(), new_value); From beb072a8938db93e694435e852510b79a0909fd3 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 21:45:21 +0200 Subject: [PATCH 5/6] empty lines --- src/libcore/cell.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 1610e89a82d73..1e7c8dfce5b3b 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -187,9 +187,12 @@ use ops::{Deref, DerefMut, CoerceUnsized}; use ptr; /// A mutable memory location. +/// /// # Examples +/// /// Here you can see how using `Cell` allows to use mutable field inside /// immutable struct (which is also called 'interior mutability'). +/// /// ``` /// use std::cell::Cell; /// @@ -204,8 +207,10 @@ use ptr; /// }; /// /// let new_value = 100; +/// /// // ERROR, because my_struct is immutable /// // immutable.regular_field = new_value; +/// /// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell /// immutable.special_field.set(new_value); /// assert_eq!(immutable.special_field.get(), new_value); From d429a4eac81aea6070655cdfb5604187d94355a2 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Mon, 24 Jul 2017 23:43:34 +0200 Subject: [PATCH 6/6] s/immutable/my_struct --- src/libcore/cell.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 1e7c8dfce5b3b..21b5557db99f2 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -209,11 +209,11 @@ use ptr; /// let new_value = 100; /// /// // ERROR, because my_struct is immutable -/// // immutable.regular_field = new_value; +/// // my_struct.regular_field = new_value; /// /// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell -/// immutable.special_field.set(new_value); -/// assert_eq!(immutable.special_field.get(), new_value); +/// my_struct.special_field.set(new_value); +/// assert_eq!(my_struct.special_field.get(), new_value); /// ``` /// /// See the [module-level documentation](index.html) for more.