Struct tqdb::Database [−][src]
pub struct Database<T>(_);Expand description
A database stores many items in an unordered fashion.
Examples
We can create a database using any type.
use tqdb::Database;
let db1: Database<i32> = Database::new();
let db2: Database<&u8> = Database::from_iter("hello world".as_bytes().into_iter());
struct Vec2 { x: i32, y: i32 };
let db3: Database<Vec2> = Database::from_iter(vec![ Vec2 { x: 0, y: 5 }, Vec2 { x: 100, y: 50 } ]);If the type is serializable, we can even save it as json!
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Vec2 { x: i32, y: i32 };
use tqdb::Database;
let db1: Database<i32> = Database::new();
let db2: Database<&u8> = Database::from_iter("hello world".as_bytes().into_iter());
let db3: Database<Vec2> = Database::from_iter(vec![ Vec2 { x: 0, y: 5 }, Vec2 { x: 100, y: 50 } ]);
db1.save_to_file("db1.json")?;
db2.save_to_file("db2.json")?;
db3.save_to_file("db3.json")?;We can query a database using macros!
We can search a database…
use tqdb::{Database, Query, search, search_mut, remove};
let db = Database::from_iter(1..10);
let found_items = search!(&db match |it: &i32| *it >= 5 && *it <= 7);…search a database (with mutable access)
let found_items_mut1 = search_mut!(&mut db match |it: &i32| *it >= 5 && *it <= 7);
// or
let found_items_mut2 = search!(&mut db match |it: &i32| *it >= 5 && *it <= 7);…and remove items easily!
let removed_items = remove!(&mut db match |it: &i32| *it >= 5 && *it <= 7);Implementations
We can search a database with a Query. Usually done with the search! macro.
Examples
Simple query
use tqdb::{Database, Query};
let db = Database::from_iter(1..10);
assert!([&1,&2,&3,&4].into_iter().eq(db.search(Query::new(|it: &i32| *it < 5))));We can also chain together queries with the & and | operators
// cool AND query
assert!([&3,&4].into_iter().eq(
db.search( Query::new(|it: &i32| *it < 5) & Query::new(|it: &i32| *it > 2) )));
// cool OR query
assert!([&1,&2,&5,&6,&7,&8,&9].into_iter().eq(
db.search( Query::new(|it: &i32| *it >= 5) | Query::new(|it: &i32| *it <= 2) )));We can search a database with a Query. Usually done with the search_mut! macro.
Examples
Simple query
use tqdb::{Database, Query};
let mut db = Database::from_iter(1..10);
assert!([&mut 1,&mut 2,&mut 3,&mut 4].into_iter().eq(
db.search_mut(Query::new(|it: &i32| *it < 5))));We can also chain together queries with the & and | operators
// cool AND query
assert!([&mut 3,&mut 4].into_iter().eq(
db.search( Query::new(|it: &i32| *it < 5) & Query::new(|it: &i32| *it > 2) )));
// cool OR query
assert!([&mut 1,&mut 2,&mut 5,&mut 6,&mut 7,&mut 8,&mut 9].into_iter().eq(
db.search( Query::new(|it: &i32| *it > 4) | Query::new(|it: &i32| *it < 3) )));We can insert an item into a database
Example
use tqdb::Database;
let mut db: Database<i32> = Database::new();
db.insert(0);We can insert a unique item into a database
Raises
May raise an DatabaseError::DuplicateItemInsertion if trying to insert a duplicate item.
Example
use tqdb::Database;
let mut db: Database<i32> = Database::new();
assert!(db.insert_unique(0).is_ok());
assert!(db.insert_unique(1).is_ok());
assert!(db.insert_unique(0).is_err());
assert!(db.insert_unique(1).is_err());
assert!(db.insert_unique(2).is_ok());We can remove an item from a database with a Query. Usually done with remove! macro.
Examples
Simple query
use tqdb::{Database, Query};
let mut db = Database::from_iter(1..10);
assert!([1,2,3,4].into_iter().eq(
db.remove(Query::new(|it: &i32| *it < 5))));
assert!([&5,&6,&7,&8,&9].into_iter().eq(
db.iter()));Save the database to a Writer
Raises:
May raise a DatabaseError::BadWrite if we could not successfully write to the writer.
Examples
// given a simple writer
struct StringWriter(String);
impl std::io::Write for StringWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.write_str(std::str::from_utf8(buf).unwrap()).unwrap();
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let db = Database::from_iter(1..10);
let mut output = StringWriter { 0: String::new() };
assert!(db.save(&mut output).is_ok());
assert_eq!("[1,2,3,4,5,6,7,8,9]", output.0.as_str());pub fn save_to_file<P: AsRef<Path>>(&self, p: P) -> Result<(), DatabaseError> where
T: Serialize,
pub fn save_to_file<P: AsRef<Path>>(&self, p: P) -> Result<(), DatabaseError> where
T: Serialize,
Allows us to save the database as json to a file.
Raises:
May raise a DatabaseError::BadWrite if we could not successfully write to the file.
Examples:
use tqdb::Database;
let db: Database<i32> = Database::from_iter(1..10);
db.save_to_file("test.db.json")?;Lets us see all the items in our database
Examples
use tqdb::Database;
let db: Database<i32> = Database::from_iter(1..10);
for item in db.iter() {
println!("Item is {}!", item);
}Allows us to see all of the items in our database and mutate them
Examples
use tqdb::Database;
let mut db: Database<i32> = Database::from_iter(1..10);
for mut item in db.iter_mut() {
println!("Item was {}!", item);
*item += 5;
println!("Item is now {}!", item);
}Trait Implementations
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Extends a collection with the contents of an iterator. Read more
extend_one)Extends a collection with exactly one element.
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
type Error = DatabaseError
type Error = DatabaseError
The type returned in the event of a conversion error.
Performs the conversion.
Auto Trait Implementations
impl<T> RefUnwindSafe for Database<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Database<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
