Skip to main content

Namespace

Struct Namespace 

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

The part of a Store that one part of the bot uses.

Get one with Store::namespace. Clones share the same connection.

Implementations§

Source§

impl Namespace

Source

pub fn name(&self) -> &str

The name of the namespace.

Source

pub async fn sql<R, F>(&self, f: F) -> Result<R, StoreError>
where R: Send + 'static, F: FnOnce(&mut Connection) -> Result<R> + Send + 'static,

Run f with the database connection, on Tokio’s blocking pool.

f gets the full rusqlite::Connection, so it can prepare statements and start transactions. Calls from all namespaces of a store run one at a time. A call that waits for the connection does not occupy a thread of the blocking pool.

All namespaces of a store share this connection. The store sets its connection-wide settings: the journal mode, the busy timeout, and the authorizer, which Namespace::migrate uses while it runs. Do not change these settings in f. For example, migrate removes an authorizer that f installs.

§Errors

Returns StoreError::Sql if f returns an error, and StoreError::Task if f panics.

Source

pub async fn get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, StoreError>

Get the value of key, decoded from JSON into T.

Returns None if the namespace has no value for key.

§Errors

Returns StoreError::Decode if the stored value does not decode into T, and StoreError::Sql if SQLite cannot read it.

Source

pub async fn set<T: Serialize + ?Sized>( &self, key: &str, value: &T, ) -> Result<(), StoreError>

Set the value of key to value, encoded as JSON.

A value that key already has is replaced.

§Errors

Returns StoreError::Encode if value does not encode as JSON, and StoreError::Sql if SQLite cannot write it.

Source

pub async fn delete(&self, key: &str) -> Result<bool, StoreError>

Delete the value of key.

Returns true if key had a value.

§Errors

Returns StoreError::Sql if SQLite cannot delete the value.

Source

pub async fn keys(&self, prefix: &str) -> Result<Vec<String>, StoreError>

The keys that start with prefix, in byte order.

The comparison uses the bytes of prefix. Thus % and _ have no special meaning, and a key that contains a NUL also matches. An empty prefix gives all keys of the namespace.

§Errors

Returns StoreError::Sql if SQLite cannot read the keys.

Source

pub async fn migrate(&self, steps: &[&str]) -> Result<(), StoreError>

Apply the steps of steps that the database does not have yet.

The store records how many steps each namespace has. A call applies only the steps after that number, in order, and records the new number. Thus it is safe to call this at each start of the bot with the full list.

A step can hold more than one SQL statement. All steps of one call run in one transaction: if a step fails, the call applies no step. Thus a step cannot use a statement that SQLite refuses in a transaction, for example VACUUM. A step also cannot use BEGIN, COMMIT, END or ROLLBACK, because these end the transaction of the call. The call refuses such a step. SAVEPOINT, RELEASE and ROLLBACK TO are permitted.

§Errors

Returns StoreError::Migration if a step fails or controls the transaction, and StoreError::UnknownMigrations if the database has more steps than steps.

Trait Implementations§

Source§

impl Clone for Namespace

Source§

fn clone(&self) -> Self

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
Source§

impl Debug for Namespace

Source§

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

Formats the value using the given formatter. 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more