Skip to main content

ValRef

Struct ValRef 

Source
pub struct ValRef { /* private fields */ }
Expand description

A reference to a value in the storage engine.

Implementations§

Source§

impl ValRef

Source

pub fn slice(&self) -> &[u8]

Returns the data as a byte slice.

Examples found in repository?
examples/demo.rs (line 25)
3fn main() -> Result<(), OpCode> {
4    let path = std::env::temp_dir().join("mace");
5    let _ = std::fs::remove_dir_all(&path);
6    let opt = Options::new(path).validate()?;
7    let db = Mace::new(opt)?;
8    let bucket = db.new_bucket("test", BucketOptions::default())?;
9
10    // start a read-write transaction
11    let kv = bucket.begin()?;
12    kv.put("foo", "bar")?;
13    kv.put("fool", "+1s")?;
14    kv.put("foolish", "elder")?;
15
16    // can't create two identical keys
17    let r = kv.put("foolish", "114514").err();
18    assert_eq!(r.unwrap(), OpCode::AbortTx);
19
20    // use `update` for exist key or use `upsert` when unsure
21    let r = kv.update("foolish", "114514");
22    assert!(r.is_ok());
23
24    let r = kv.get("foo")?;
25    assert_eq!(r.slice(), "bar".as_bytes());
26    kv.del("foolish")?;
27    kv.commit()?;
28
29    // rollback
30    let kv = bucket.begin()?;
31    kv.put("mo", "ha")?;
32    drop(kv);
33
34    // start a read-only transaction
35    let view = bucket.view()?;
36    let r = view.get("foo")?;
37    assert_eq!(r.slice(), "bar".as_bytes());
38    let r = view.get("mo");
39    assert_eq!(r.err().unwrap(), OpCode::NotFound);
40
41    // prefix scan
42    let r = view.get("foolish");
43    assert!(r.is_err() && r.err().unwrap() == OpCode::NotFound);
44    let iter = view.seek("foo");
45    assert_eq!(iter.count(), 2);
46
47    Ok(())
48}
Source

pub fn to_vec(self) -> Vec<u8>

Converts the reference into a owned Vec.

Trait Implementations§

Source§

impl Clone for ValRef

Source§

fn clone(&self) -> ValRef

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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<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<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, 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.