Skip to main content

Ref

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>

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§

§

impl<T: Debug> Debug for Ref<'_, T>

§

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

Formats the value using the given formatter. Read more
§

impl<T> Deref for Ref<'_, T>

§

type Target = T

The resulting type after dereferencing.
§

fn deref(&self) -> &T

Dereferences the value.

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>
where T: Sync + Send,

§

impl<'txn, T> Sync for Ref<'txn, T>
where T: Sync + Send,

§

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> 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> 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<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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, 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.