Struct Ref

Source
pub struct Ref<T> { /* private fields */ }

Implementations§

Source§

impl<T> Ref<T>

Source

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
Hide additional 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}
Source

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}
Source

pub fn lock(&self)

§Dont use!!!
Source

pub fn unlock(&self)

§Dont use!!!
Source

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
Hide additional 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}
Source

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
Hide additional 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§

Source§

impl<T> Clone for Ref<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Ref<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Ref<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> Display for Ref<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Send for Ref<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.