pub struct Nullable<T>where
T: NotNull,{ /* private fields */ }Expand description
Type implementation for a nullable column.
The T parameter must be a NotNull type marker. This constraint
prevents assymetric types from being defined.
So something like this is supported:
ty::Nullable<ty::Integer>While something like this is denied at compile time:
ⓘ
// This will not compile because Nullable does not implement `NotNull`.
ty::Nullable<ty::Nullable<ty::Integer>>;§Examples
use sqll::{Connection, FromColumn, Result, Statement};
use sqll::ty;
#[derive(Debug, PartialEq, Eq)]
struct MyOptional(Option<u32>);
impl FromColumn<'_> for MyOptional {
type Type = ty::Nullable<ty::Integer>;
#[inline]
fn from_column(stmt: &Statement, index: ty::Nullable<ty::Integer>) -> Result<Self> {
Ok(MyOptional(<_>::from_column(stmt, index)?))
}
}
let mut c = Connection::open_in_memory()?;
c.execute(r#"
CREATE TABLE test (value INTEGER);
INSERT INTO test (value) VALUES (42), (NULL);
"#)?;
let mut stmt = c.prepare("SELECT value FROM test")?;
assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(Some(42))));
assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(None)));
assert_eq!(stmt.next::<MyOptional>()?, None);Trait Implementations§
Source§impl<T> Type for Nullable<T>where
T: NotNull,
Type implementation for a nullable column.
impl<T> Type for Nullable<T>where
T: NotNull,
Type implementation for a nullable column.
§Examples
use sqll::{Connection, FromColumn, Result, Statement};
use sqll::ty;
#[derive(Debug, PartialEq, Eq)]
struct MyOptional(Option<u32>);
impl FromColumn<'_> for MyOptional {
type Type = ty::Nullable<ty::Integer>;
#[inline]
fn from_column(stmt: &Statement, index: ty::Nullable<ty::Integer>) -> Result<Self> {
Ok(MyOptional(<_>::from_column(stmt, index)?))
}
}
let mut c = Connection::open_in_memory()?;
c.execute(r#"
CREATE TABLE test (value INTEGER);
INSERT INTO test (value) VALUES (42), (NULL);
"#)?;
let mut stmt = c.prepare("SELECT value FROM test")?;
assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(Some(42))));
assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(None)));
assert_eq!(stmt.next::<MyOptional>()?, None);Auto Trait Implementations§
impl<T> Freeze for Nullable<T>where
T: Freeze,
impl<T> RefUnwindSafe for Nullable<T>where
T: RefUnwindSafe,
impl<T> Send for Nullable<T>where
T: Send,
impl<T> Sync for Nullable<T>where
T: Sync,
impl<T> Unpin for Nullable<T>where
T: Unpin,
impl<T> UnsafeUnpin for Nullable<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Nullable<T>where
T: UnwindSafe,
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