SqlVec

Struct SqlVec 

Source
pub struct SqlVec<T: ToString + FromStr>(/* private fields */);
Expand description

A generic container for vectors whose contents implement ToString & FromStr.

SqlVec implements ToSql & FromSql storing values as \u{F1} delimited text, allowing for SQL operations.

§Example

 use sqlvec::SqlVec;
 use rusqlite::{Error, Connection, params};

 let conn = Connection::open_in_memory().unwrap();

 // Create a table with a column that uses our custom type.
 conn.execute(
     "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, data TEXT);",
     [],
 ).unwrap();

 // Insert a SqlVec into the table.
 let values = SqlVec::new(vec!["one".to_string(), "two".to_string()]);
 conn.execute(
     "INSERT INTO test (data) VALUES (?1)",
     params![values],
 ).unwrap();

 // Retrieve the SqlVec from the table.
 let mut stmt = conn.prepare("SELECT data FROM test WHERE id = ?1").unwrap();
 let mut rows = stmt.query(params![1]).unwrap();
 let row = rows.next().unwrap().unwrap();
 let db_values: SqlVec<String> = row.get(0).unwrap();

 // Assert that the retrieved SqlVec matches the original.
 assert_eq!(values, db_values);

Implementations§

Source§

impl<T: ToString + FromStr> SqlVec<T>

Source

pub fn new<I: IntoIterator<Item = T>>(items: I) -> Self

Creates a new SqlVec from an iterable collection of items.

§Example
use sqlvec::SqlVec;

let vec = SqlVec::new([1, 2, 3]);
Source

pub fn into_inner(self) -> Vec<T>

Consumes the SqlVec, returning its internal vector.

This method allows you to take ownership of the underlying vector contained within the SqlVec. After calling into_inner, the SqlVec cannot be used anymore unless recreated.

§Example
use sqlvec::SqlVec;

let sql_vec = SqlVec::new(vec![1, 2]);
let vec = sql_vec.into_inner();

assert_eq!(vec, vec![1, 2]);
Source

pub fn inner(&self) -> &Vec<T>

Returns a borrowed reference to the internal vector.

§Example
use sqlvec::SqlVec;

let sql_vec = SqlVec::new(vec![1, 2]);
let vec_ref = sql_vec.inner();


assert_eq!(vec_ref, &vec![1, 2]);

Trait Implementations§

Source§

impl<T: Clone + ToString + FromStr> Clone for SqlVec<T>

Source§

fn clone(&self) -> SqlVec<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + ToString + FromStr> Debug for SqlVec<T>

Source§

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

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

impl<T: ToString + FromStr> Default for SqlVec<T>

Source§

fn default() -> Self

Unlike Vec, SqlVec does not require T to implement the Default trait.

§Example
use sqlvec::SqlVec;

use std::str::FromStr;

struct MyType {
    value: i32,
}

impl ToString for MyType {
    fn to_string(&self) -> String {
        self.value.to_string()
    }
}

impl FromStr for MyType {
    type Err = std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse::<i32>().map(|value| MyType { value })
    }
}

let default: SqlVec<MyType> = SqlVec::default();
Source§

impl<T: ToString + FromStr> FromIterator<T> for SqlVec<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: ToString + FromStr> FromSql for SqlVec<T>

Source§

fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self>

Converts SQLite value into Rust value.
Source§

impl FromStr for SqlVec<String>

Source§

type Err = Infallible

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<T: PartialEq + ToString + FromStr> PartialEq for SqlVec<T>

Source§

fn eq(&self, other: &SqlVec<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: ToString + FromStr> ToSql for SqlVec<T>

Source§

fn to_sql(&self) -> Result<ToSqlOutput<'_>>

Converts Rust value to SQLite value
Source§

impl<T: ToString + FromStr> ToString for SqlVec<T>

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T: Eq + ToString + FromStr> Eq for SqlVec<T>

Source§

impl<T: ToString + FromStr> StructuralPartialEq for SqlVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for SqlVec<T>

§

impl<T> RefUnwindSafe for SqlVec<T>
where T: RefUnwindSafe,

§

impl<T> Send for SqlVec<T>
where T: Send,

§

impl<T> Sync for SqlVec<T>
where T: Sync,

§

impl<T> Unpin for SqlVec<T>
where T: Unpin,

§

impl<T> UnwindSafe for SqlVec<T>
where T: UnwindSafe,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> 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 = 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.