FixedBytes

Struct FixedBytes 

Source
pub struct FixedBytes<const N: usize> { /* private fields */ }
Expand description

A helper to read at most a fixed number of N bytes from a column. This allocates the storage for the bytes read on the stack.

Implementations§

Source§

impl<const N: usize> FixedBytes<N>

Source

pub fn into_bytes(self) -> Option<[u8; N]>

Coerce into the underlying bytes if all of them have been initialized.

§Examples
use sqlite_ll::{Connection, State, FixedBytes};

let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (id BLOB);
INSERT INTO users (id) VALUES (X'01020304'), (X'05060708');
"##)?;

let mut stmt = c.prepare("SELECT id FROM users")?;

while let State::Row = stmt.step()? {
    let bytes = stmt.read::<FixedBytes<4>>(0)?;
    assert!(matches!(bytes.into_bytes(), Some([1, 2, 3, 4] | [5, 6, 7, 8])));
}
Source

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

Coerce into the slice of initialized memory which is present.

§Examples
use sqlite_ll::{Connection, State, FixedBytes};

let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (id BLOB);
INSERT INTO users (id) VALUES (X'01020304'), (X'0506070809');
"##)?;

let mut stmt = c.prepare("SELECT id FROM users")?;

while let State::Row = stmt.step()? {
    let bytes = stmt.read::<FixedBytes<10>>(0)?;
    assert!(matches!(bytes.as_bytes(), &[1, 2, 3, 4] | &[5, 6, 7, 8, 9]));
}

Trait Implementations§

Source§

impl<const N: usize> Debug for FixedBytes<N>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Readable for FixedBytes<N>

Readable implementation for FixedBytes which reads at most N bytes.

If the column contains more than N bytes, a [Code::MISMATCH] error is returned.

§Examples

use sqlite_ll::{Connection, State, FixedBytes, Code};

let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (id BLOB);
INSERT INTO users (id) VALUES (X'01020304'), (X'0506070809');
"##)?;

let mut stmt = c.prepare("SELECT id FROM users")?;

assert_eq!(stmt.step()?, State::Row);
let bytes = stmt.read::<FixedBytes<4>>(0)?;
assert_eq!(bytes.as_bytes(), &[1, 2, 3, 4]);

assert_eq!(stmt.step()?, State::Row);
let e = stmt.read::<FixedBytes<4>>(0).unwrap_err();
assert_eq!(e.code(), Code::MISMATCH);

let bytes = stmt.read::<FixedBytes<5>>(0)?;
assert_eq!(bytes.as_bytes(), &[5, 6, 7, 8, 9]);

Auto Trait Implementations§

§

impl<const N: usize> Freeze for FixedBytes<N>

§

impl<const N: usize> RefUnwindSafe for FixedBytes<N>

§

impl<const N: usize> Send for FixedBytes<N>

§

impl<const N: usize> Sync for FixedBytes<N>

§

impl<const N: usize> Unpin for FixedBytes<N>

§

impl<const N: usize> UnwindSafe for FixedBytes<N>

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