pub struct AvlonDB {
pub db_name: String,
pub client: Db,
}Expand description
A struct representing a wrapper around a Sled database.
Fields§
§db_name: String§client: DbImplementations§
source§impl AvlonDB
impl AvlonDB
sourcepub fn new(db_name: String) -> Self
pub fn new(db_name: String) -> Self
Creates a new instance of AvlonDB with the specified database name.
§Arguments
db_name- The name of the database file.
§Returns
A new AvlonDB instance.
Examples found in repository?
examples/basic_usage.rs (line 24)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define the database name
let db_name = String::from("test_db");
// Create an instance of Account
let account = Account {
username: String::from("johndoe"),
password: String::from("secretpassword"),
age: 30,
};
// Initialize the database
let db = AvlonDB::new(db_name);
// Save the account to the database
db.save(account.username.clone(), account)?;
println!("Account saved to database!");
// Load the account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Update the account in the database
let updated_account = Account {
username: String::from("joker"),
password: String::from("123654987"),
age: 99,
};
db.update("johndoe", updated_account)?;
// Load the updated account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Updated account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Remove the account from the database
db.remove("johndoe")?;
// Attempt to load the account again after removal
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!(
"Account loaded from database after removal: {:?}",
loaded_account
);
} else {
println!("Account successfully removed from the database.");
}
Ok(())
}sourcepub fn save<T>(&self, key: String, value: T) -> Result<(), Box<dyn Error>>where
T: Serialize,
pub fn save<T>(&self, key: String, value: T) -> Result<(), Box<dyn Error>>where
T: Serialize,
Saves a value to the database under the given key.
§Arguments
key- The key to associate with the value.value- The value to store in the database.
§Returns
A Result indicating success or failure.
Examples found in repository?
examples/basic_usage.rs (line 27)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define the database name
let db_name = String::from("test_db");
// Create an instance of Account
let account = Account {
username: String::from("johndoe"),
password: String::from("secretpassword"),
age: 30,
};
// Initialize the database
let db = AvlonDB::new(db_name);
// Save the account to the database
db.save(account.username.clone(), account)?;
println!("Account saved to database!");
// Load the account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Update the account in the database
let updated_account = Account {
username: String::from("joker"),
password: String::from("123654987"),
age: 99,
};
db.update("johndoe", updated_account)?;
// Load the updated account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Updated account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Remove the account from the database
db.remove("johndoe")?;
// Attempt to load the account again after removal
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!(
"Account loaded from database after removal: {:?}",
loaded_account
);
} else {
println!("Account successfully removed from the database.");
}
Ok(())
}sourcepub fn load<T>(&self, key: &str) -> Result<Option<T>, Box<dyn Error>>where
T: for<'de> Deserialize<'de>,
pub fn load<T>(&self, key: &str) -> Result<Option<T>, Box<dyn Error>>where
T: for<'de> Deserialize<'de>,
Loads a value from the database associated with the given key.
§Arguments
key- The key associated with the value to load.
§Returns
A Result containing an Option with the value, or None if the key does not exist.
Examples found in repository?
examples/basic_usage.rs (line 31)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define the database name
let db_name = String::from("test_db");
// Create an instance of Account
let account = Account {
username: String::from("johndoe"),
password: String::from("secretpassword"),
age: 30,
};
// Initialize the database
let db = AvlonDB::new(db_name);
// Save the account to the database
db.save(account.username.clone(), account)?;
println!("Account saved to database!");
// Load the account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Update the account in the database
let updated_account = Account {
username: String::from("joker"),
password: String::from("123654987"),
age: 99,
};
db.update("johndoe", updated_account)?;
// Load the updated account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Updated account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Remove the account from the database
db.remove("johndoe")?;
// Attempt to load the account again after removal
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!(
"Account loaded from database after removal: {:?}",
loaded_account
);
} else {
println!("Account successfully removed from the database.");
}
Ok(())
}sourcepub fn remove(&self, key: &str) -> Result<(), Box<dyn Error>>
pub fn remove(&self, key: &str) -> Result<(), Box<dyn Error>>
Removes a value from the database associated with the given key.
§Arguments
key- The key associated with the value to remove.
§Returns
A Result indicating success or failure.
Examples found in repository?
examples/basic_usage.rs (line 53)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define the database name
let db_name = String::from("test_db");
// Create an instance of Account
let account = Account {
username: String::from("johndoe"),
password: String::from("secretpassword"),
age: 30,
};
// Initialize the database
let db = AvlonDB::new(db_name);
// Save the account to the database
db.save(account.username.clone(), account)?;
println!("Account saved to database!");
// Load the account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Update the account in the database
let updated_account = Account {
username: String::from("joker"),
password: String::from("123654987"),
age: 99,
};
db.update("johndoe", updated_account)?;
// Load the updated account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Updated account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Remove the account from the database
db.remove("johndoe")?;
// Attempt to load the account again after removal
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!(
"Account loaded from database after removal: {:?}",
loaded_account
);
} else {
println!("Account successfully removed from the database.");
}
Ok(())
}sourcepub fn update<T>(&self, key: &str, new_value: T) -> Result<(), Box<dyn Error>>where
T: Serialize,
pub fn update<T>(&self, key: &str, new_value: T) -> Result<(), Box<dyn Error>>where
T: Serialize,
Updates the value associated with the given key in the database.
§Arguments
key- The key associated with the value to update.new_value- The new value to store in the database.
§Returns
A Result indicating success or failure. Returns an error if the key does not exist.
Examples found in repository?
examples/basic_usage.rs (line 43)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define the database name
let db_name = String::from("test_db");
// Create an instance of Account
let account = Account {
username: String::from("johndoe"),
password: String::from("secretpassword"),
age: 30,
};
// Initialize the database
let db = AvlonDB::new(db_name);
// Save the account to the database
db.save(account.username.clone(), account)?;
println!("Account saved to database!");
// Load the account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Update the account in the database
let updated_account = Account {
username: String::from("joker"),
password: String::from("123654987"),
age: 99,
};
db.update("johndoe", updated_account)?;
// Load the updated account from the database
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!("Updated account loaded from database: {:?}", loaded_account);
} else {
println!("Account not found in the database.");
}
// Remove the account from the database
db.remove("johndoe")?;
// Attempt to load the account again after removal
if let Some(loaded_account) = db.load::<Account>("johndoe")? {
println!(
"Account loaded from database after removal: {:?}",
loaded_account
);
} else {
println!("Account successfully removed from the database.");
}
Ok(())
}Auto Trait Implementations§
impl Freeze for AvlonDB
impl !RefUnwindSafe for AvlonDB
impl Send for AvlonDB
impl Sync for AvlonDB
impl Unpin for AvlonDB
impl !UnwindSafe for AvlonDB
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