Struct ord_lmdb_zero::ConstTransaction
source · [−]pub struct ConstTransaction<'env> { /* private fields */ }
Expand description
Base functionality for an LMDB transaction.
The type is “const” in a similar usage to the modifier in C: One cannot use
it to make any modifications, but also cannot rely on it actually being
read-only. ConstTransaction
s are used to write code that can operate in
either kind of transaction.
Unlike most other LMDB wrappers, transactions here are (indirectly) the things in control of accessing data behind cursors. This is in order to correctly express memory semantics: Moving a cursor does not invalidate memory obtained from the cursor; however, any mutation through the same transaction does. We therefore model accesses to data in the environment as borrows of the transaction and the database themselves (possibly mutable on the latter), which allows the borrow checker to ensure that all references are dropped before doing a structural modification.
Note that due to limitations in the Rust borrow checker, one actually needs
to use the *Accessor
structs to access data. Any transaction will yield
at most one accessor, which is implemented with a runtime check that should
in the vast majority of cases get optimised out.
Mutability of a transaction reference does not indicate mutability of the underlying database, but rather exclusivity for enforcement of child transaction semantics.
Ownership
Transactions support all three ownership modes (but owned mode is not
useful). See ReadTransaction
and WriteTransaction
for details.
Lifetime
A ConstTransaction
must be strictly outlived by its Environment
.
'env
is covariant: given two lifetimes 'x
and 'y
where 'x: 'y
, a
&ConstTransaction<'x>
will implicitly coerce to &ConstTransaction<'y>
.
fn convariance<'x, 'y>(db: &lmdb::ConstTransaction<'x>)
where 'x: 'y {
let _db2: &lmdb::ConstTransaction<'y> = db;
}
Because of this property, if you need to hold onto an
&lmdb::ConstTransaction
and must explicitly name both lifetimes,
it is usually best to use the same lifetime for both the reference and the
parameter, eg &'x lmdb::ConstTransaction<'x>
.
Implementations
sourceimpl<'env> ConstTransaction<'env>
impl<'env> ConstTransaction<'env>
sourcepub fn access(&self) -> ConstAccessor<'_>
pub fn access(&self) -> ConstAccessor<'_>
Returns an accessor used to manipulate data in this transaction.
Ownership
Unlike most other lmdb-zero APIs, accessors do not support shared
ownership modes (e.g., where the accessor would hold on to a
Rc<ConstTransaction>
). If you need dynamically-managed lifetime,
instead simply drop the accessor and get a new one the next time one is
needed.
Panics
Panics if this function has already been called on this transaction and the returned value has not yet been dropped.
Example
let txn = lmdb::ReadTransaction::new(&env).unwrap();
// Get access the first time
let access = txn.access();
// You can't get the accessor again in the same scope, since this
// would create two references to the same logical memory and allow
// creating aliased mutable references and so forth.
let access2 = txn.access(); // PANIC!
sourcepub fn cursor<'txn, 'db, DB>(&'txn self, db: DB) -> Result<Cursor<'txn, 'db>> where
DB: Into<Supercow<'db, Database<'db>>>,
pub fn cursor<'txn, 'db, DB>(&'txn self, db: DB) -> Result<Cursor<'txn, 'db>> where
DB: Into<Supercow<'db, Database<'db>>>,
Creates a new cursor scoped to this transaction, bound to the given database.
This method is functionally equivalent to the method on CreateCursor
and exists for convenience and backwards-compatibility.
If you have an, e.g., Rc<ReadTransaction>
and want to get a
Cursor<'static,'db>
, make sure you have the CreateCursor
trait
imported so that the needed alternate implementations of this method
are available.
Trait Implementations
sourceimpl<'txn, 'env: 'txn> CreateCursor<'txn> for &'txn ConstTransaction<'env>
impl<'txn, 'env: 'txn> CreateCursor<'txn> for &'txn ConstTransaction<'env>
Auto Trait Implementations
impl<'env> !RefUnwindSafe for ConstTransaction<'env>
impl<'env> !Send for ConstTransaction<'env>
impl<'env> !Sync for ConstTransaction<'env>
impl<'env> Unpin for ConstTransaction<'env>
impl<'env> !UnwindSafe for ConstTransaction<'env>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> SafeBorrow<T> for T where
T: ?Sized,
impl<T> SafeBorrow<T> for T where
T: ?Sized,
sourcefn borrow_replacement(ptr: &T) -> &T
fn borrow_replacement(ptr: &T) -> &T
Given ptr
, which was obtained from a prior call to Self::borrow()
,
return a value with the same nominal lifetime which is guaranteed to
survive mutations to Self
. Read more