Crates.io | cura |
lib.rs | cura |
version | 0.8.0 |
source | src |
created_at | 2023-04-24 19:52:51.545601 |
updated_at | 2023-05-16 12:43:39.224793 |
description | arc-lock-system that should clean up the code a lot |
homepage | |
repository | https://github.com/itmo/cura |
max_upload_size | |
id | 847827 |
size | 28,116 |
That aims to be simple and straightforward to use when sharing data between threads.
An attempt at creating an Arc-RwLock combination that is straightforward to use and no hassle , instead of worrying about being fast and lean.
use cura::Cura;
trait Foo:Send+Sync
{
fn get(&self)->i32;
fn set(&mut self,i:i32);
}
#[derive(Debug)]
struct FF{i:i32,};
struct BB{i:i32};
impl Foo for FF{
fn get(&self)->i32{return self.i;}
fn set(&mut self,i:i32){self.i=i;}
}
impl Foo for BB{
fn get(&self)->i32{return self.i;}
fn set(&mut self,i:i32){self.i=i;}
}
let t=FF{i:1};
// you can do straight "from_box" but currently its impossible to
// "alter" unsized types
let foo2:Cura<dyn Foo>=Cura::from_box(Box::new(FF{i:2}));
let foo:Cura<Box<dyn Foo>>=Cura::new(Box::new(t));
let a=foo.clone();
let b=foo.clone();
{
assert_eq!(a.read().get(),1);
{
a.alter(|s|{
s.set(2);
Some(())
});
}
{
a.alter(|s|{ //this only works for Sized types
*s=Box::new(BB{i:2});
Some(())
});
}
let lock=a.read();
let v=lock;
assert_eq!(v.get(),2)
}//lock dropped here
{
(*b.write()).set(3); //lock dropped here i think
}
assert_eq!((*a.read()).get(),3);