Struct Ref
pub struct Ref<'txn, T> { /* private fields */ }Expand description
A snapshot-consistent view of a record.
Holds the version alive, so it stays valid for as long as you keep it, even as other transactions overwrite the record.
Implementations§
§impl<T: Clone> Ref<'_, T>
impl<T: Clone> Ref<'_, T>
pub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Copy the record out, detaching it from the version chain.
Examples found in repository?
examples/basic.rs (line 107)
24fn main() -> Result<()> {
25 let db = Database::open(Config::in_memory())?;
26 db.register::<Account>()?;
27
28 // ---- insert -----------------------------------------------------------
29 // `transaction` runs the closure, commits it, and retries it if it hits a
30 // retriable conflict. Snapshot isolation by default.
31 db.transaction(|tx| {
32 tx.insert(Account {
33 id: 1,
34 owner: "ada".into(),
35 branch: 10,
36 balance: 500,
37 })?;
38 tx.insert(Account {
39 id: 2,
40 owner: "bob".into(),
41 branch: 10,
42 balance: 250,
43 })?;
44 tx.insert(Account {
45 id: 3,
46 owner: "cleo".into(),
47 branch: 20,
48 balance: 900,
49 })?;
50 Ok(())
51 })?;
52
53 // ---- read -------------------------------------------------------------
54 let mut tx = db.begin();
55 let ada = tx.get::<Account>(&1)?.expect("just inserted");
56 println!("ada: branch {}, balance {}", ada.branch, ada.balance);
57
58 // A read-only transaction has nothing to commit; dropping it rolls back,
59 // which for a reader means simply releasing its snapshot.
60 drop(tx);
61
62 // ---- update -----------------------------------------------------------
63 db.transaction(|tx| {
64 tx.update::<Account>(&1, |a| a.balance -= 100)?;
65 tx.update::<Account>(&2, |a| a.balance += 100)?;
66 Ok(())
67 })?;
68
69 // ---- scan by primary key ----------------------------------------------
70 let mut tx = db.begin();
71 println!("\nall accounts:");
72 for account in tx.scan::<Account>()? {
73 println!(
74 " {:>4} {:<6} branch {} {:>5}",
75 account.id, account.owner, account.branch, account.balance
76 );
77 }
78
79 // ---- scan by secondary index ------------------------------------------
80 println!("\nbranch 10:");
81 for account in tx.scan_index(Account::BRANCH, 10u32..=10)? {
82 println!(" {} ({})", account.owner, account.balance);
83 }
84 drop(tx);
85
86 // ---- constraint violations --------------------------------------------
87 let mut tx = db.begin();
88 let duplicate = tx.insert(Account {
89 id: 99,
90 owner: "ada".into(),
91 branch: 30,
92 balance: 0,
93 });
94 println!("\nreusing owner 'ada': {}", duplicate.unwrap_err());
95 drop(tx);
96
97 // ---- delete -----------------------------------------------------------
98 db.transaction(|tx| {
99 let existed = tx.delete::<Account>(&3)?;
100 println!("deleted cleo: {existed}");
101 Ok(())
102 })?;
103
104 let mut tx = db.begin();
105 println!(
106 "cleo now: {:?}",
107 tx.get::<Account>(&3)?.map(|r| r.to_owned())
108 );
109
110 Ok(())
111}Trait Implementations§
Auto Trait Implementations§
impl<'txn, T> Freeze for Ref<'txn, T>
impl<'txn, T> RefUnwindSafe for Ref<'txn, T>where
T: RefUnwindSafe,
impl<'txn, T> Send for Ref<'txn, T>
impl<'txn, T> Sync for Ref<'txn, T>
impl<'txn, T> Unpin for Ref<'txn, T>
impl<'txn, T> UnsafeUnpin for Ref<'txn, T>
impl<'txn, T> UnwindSafe for Ref<'txn, T>where
T: RefUnwindSafe,
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