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());

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 construct a new empty database.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Performs the conversion.

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.