pub struct Ref<T> { /* private fields */ }
Implementations§
Source§impl<T> Ref<T>
impl<T> Ref<T>
Sourcepub fn new(data: T) -> Self
pub fn new(data: T) -> Self
Examples found in repository?
examples/with_closure.rs (line 4)
3fn main() {
4 let data = Ref::new(0);
5
6 /// in this scope can be modificated
7 data.mut_scope(|data| {
8 *data = 21;
9 });
10
11 println!("Data: {}", data);
12 assert_eq!(*data, 21);
13
14 let a = 43;
15 /// if you want to move a variabile to closure you need to make a other variabile a reference and put that
16 let tmp_a = &a;
17 ///move is to move the tmp_a to closure, this is why you dont want to move the original value because will be droped when closure ends!
18 data.mut_scope(move |data| {
19 let a = tmp_a;
20 *data = *a;
21 });
22
23 println!("{}", data);
24 assert_eq!(*data, a);
25}
More examples
examples/simple.rs (line 4)
3fn main() {
4 let data = Ref::new(0);
5
6 {
7 let mut mut_data = data.get_mut();
8 println!("Is locked: {}", data.locked());
9
10 *mut_data = 21;
11 }
12
13 // Dont do this if you do this the current thread will be stuck in a loop because noting will be droped to unlock;
14 //let mut mut_data = data.get_mut();
15 //let mut seccond_mut_data = data.get_mut();
16
17 //Never do:
18 // data.lock()
19 // data.unlock()
20 // this is very bed!
21 // do this if you really know what are you doing
22
23 let data2 = data.clone();
24 // data2 is not only the value but is has the same pointer
25 // if data2 is modificated will be applied to data
26
27 data2.mut_scope(|value| {
28 *value = 33;
29 });
30
31 // when all ref will be droped, then the data will be droped!
32
33 println!("Simple: {}", data);
34}
examples/threading.rs (line 4)
3fn main() {
4 let data = Ref::new(0);
5 let tmp_data = data.clone();
6
7 let thread_1 = std::thread::spawn(move || {
8 let data = tmp_data;
9 for _ in 0..10000000 {
10 let mut data = data.get_mut();
11 *data += 1;
12 }
13 });
14
15 let tmp_data = data.clone();
16
17 let thread_2 = std::thread::spawn(move || {
18 let data = tmp_data;
19 for _ in 0..10000000 {
20 data.mut_scope(|data| {
21 *data += 1;
22 })
23 }
24 });
25
26 let tmp_data = data.clone();
27
28 let thread_3 = std::thread::spawn(move || {
29 let data = tmp_data;
30 loop {
31 if *data == 20000000 {
32 break;
33 }
34 println!("D: {}", data);
35 }
36 });
37
38 thread_1.join().unwrap();
39 thread_2.join().unwrap();
40 thread_3.join().unwrap();
41
42 println!("Data: {}", data)
43}
Sourcepub fn locked(&self) -> bool
pub fn locked(&self) -> bool
Examples found in repository?
examples/simple.rs (line 8)
3fn main() {
4 let data = Ref::new(0);
5
6 {
7 let mut mut_data = data.get_mut();
8 println!("Is locked: {}", data.locked());
9
10 *mut_data = 21;
11 }
12
13 // Dont do this if you do this the current thread will be stuck in a loop because noting will be droped to unlock;
14 //let mut mut_data = data.get_mut();
15 //let mut seccond_mut_data = data.get_mut();
16
17 //Never do:
18 // data.lock()
19 // data.unlock()
20 // this is very bed!
21 // do this if you really know what are you doing
22
23 let data2 = data.clone();
24 // data2 is not only the value but is has the same pointer
25 // if data2 is modificated will be applied to data
26
27 data2.mut_scope(|value| {
28 *value = 33;
29 });
30
31 // when all ref will be droped, then the data will be droped!
32
33 println!("Simple: {}", data);
34}
Sourcepub fn mut_scope(&self, clasure: impl Fn(&mut T))
pub fn mut_scope(&self, clasure: impl Fn(&mut T))
Recomand to use get_mut
this is not really recomanded
but if you do things in a single thread is more recomanded
Examples found in repository?
examples/with_closure.rs (lines 7-9)
3fn main() {
4 let data = Ref::new(0);
5
6 /// in this scope can be modificated
7 data.mut_scope(|data| {
8 *data = 21;
9 });
10
11 println!("Data: {}", data);
12 assert_eq!(*data, 21);
13
14 let a = 43;
15 /// if you want to move a variabile to closure you need to make a other variabile a reference and put that
16 let tmp_a = &a;
17 ///move is to move the tmp_a to closure, this is why you dont want to move the original value because will be droped when closure ends!
18 data.mut_scope(move |data| {
19 let a = tmp_a;
20 *data = *a;
21 });
22
23 println!("{}", data);
24 assert_eq!(*data, a);
25}
More examples
examples/simple.rs (lines 27-29)
3fn main() {
4 let data = Ref::new(0);
5
6 {
7 let mut mut_data = data.get_mut();
8 println!("Is locked: {}", data.locked());
9
10 *mut_data = 21;
11 }
12
13 // Dont do this if you do this the current thread will be stuck in a loop because noting will be droped to unlock;
14 //let mut mut_data = data.get_mut();
15 //let mut seccond_mut_data = data.get_mut();
16
17 //Never do:
18 // data.lock()
19 // data.unlock()
20 // this is very bed!
21 // do this if you really know what are you doing
22
23 let data2 = data.clone();
24 // data2 is not only the value but is has the same pointer
25 // if data2 is modificated will be applied to data
26
27 data2.mut_scope(|value| {
28 *value = 33;
29 });
30
31 // when all ref will be droped, then the data will be droped!
32
33 println!("Simple: {}", data);
34}
examples/threading.rs (lines 20-22)
3fn main() {
4 let data = Ref::new(0);
5 let tmp_data = data.clone();
6
7 let thread_1 = std::thread::spawn(move || {
8 let data = tmp_data;
9 for _ in 0..10000000 {
10 let mut data = data.get_mut();
11 *data += 1;
12 }
13 });
14
15 let tmp_data = data.clone();
16
17 let thread_2 = std::thread::spawn(move || {
18 let data = tmp_data;
19 for _ in 0..10000000 {
20 data.mut_scope(|data| {
21 *data += 1;
22 })
23 }
24 });
25
26 let tmp_data = data.clone();
27
28 let thread_3 = std::thread::spawn(move || {
29 let data = tmp_data;
30 loop {
31 if *data == 20000000 {
32 break;
33 }
34 println!("D: {}", data);
35 }
36 });
37
38 thread_1.join().unwrap();
39 thread_2.join().unwrap();
40 thread_3.join().unwrap();
41
42 println!("Data: {}", data)
43}
Sourcepub fn get_mut(&self) -> RefMut<T>
pub fn get_mut(&self) -> RefMut<T>
if you doing things in as single thread if you call get_mut 2 times or more the application will be in a loop
Dont get_mut
more then one time!
Examples found in repository?
examples/simple.rs (line 7)
3fn main() {
4 let data = Ref::new(0);
5
6 {
7 let mut mut_data = data.get_mut();
8 println!("Is locked: {}", data.locked());
9
10 *mut_data = 21;
11 }
12
13 // Dont do this if you do this the current thread will be stuck in a loop because noting will be droped to unlock;
14 //let mut mut_data = data.get_mut();
15 //let mut seccond_mut_data = data.get_mut();
16
17 //Never do:
18 // data.lock()
19 // data.unlock()
20 // this is very bed!
21 // do this if you really know what are you doing
22
23 let data2 = data.clone();
24 // data2 is not only the value but is has the same pointer
25 // if data2 is modificated will be applied to data
26
27 data2.mut_scope(|value| {
28 *value = 33;
29 });
30
31 // when all ref will be droped, then the data will be droped!
32
33 println!("Simple: {}", data);
34}
More examples
examples/threading.rs (line 10)
3fn main() {
4 let data = Ref::new(0);
5 let tmp_data = data.clone();
6
7 let thread_1 = std::thread::spawn(move || {
8 let data = tmp_data;
9 for _ in 0..10000000 {
10 let mut data = data.get_mut();
11 *data += 1;
12 }
13 });
14
15 let tmp_data = data.clone();
16
17 let thread_2 = std::thread::spawn(move || {
18 let data = tmp_data;
19 for _ in 0..10000000 {
20 data.mut_scope(|data| {
21 *data += 1;
22 })
23 }
24 });
25
26 let tmp_data = data.clone();
27
28 let thread_3 = std::thread::spawn(move || {
29 let data = tmp_data;
30 loop {
31 if *data == 20000000 {
32 break;
33 }
34 println!("D: {}", data);
35 }
36 });
37
38 thread_1.join().unwrap();
39 thread_2.join().unwrap();
40 thread_3.join().unwrap();
41
42 println!("Data: {}", data)
43}
Trait Implementations§
impl<T> Send for Ref<T>
impl<T> Sync for Ref<T>
Auto Trait Implementations§
impl<T> Freeze for Ref<T>
impl<T> !RefUnwindSafe for Ref<T>
impl<T> Unpin for Ref<T>
impl<T> !UnwindSafe for Ref<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more