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>
impl<const N: usize> FixedBytes<N>
Sourcepub fn into_bytes(self) -> Option<[u8; N]>
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])));
}Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
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>
impl<const N: usize> Debug for FixedBytes<N>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more