Crates.io | renew-traits |
lib.rs | renew-traits |
version | 0.1.0 |
source | src |
created_at | 2024-09-08 20:41:34.330112 |
updated_at | 2024-09-08 20:41:34.330112 |
description | A collection of utility traits for initializing, filling, and managing collections or data structures. |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1368519 |
size | 5,674 |
renew-traits
provides a set of utility traits to manage, initialize, and extend collections or other data structures in a consistent and reusable way. The crate is designed to help you define flexible and extensible APIs for managing internal collections and structures.
ExtendWith
Extends a collection with a single item, handling potential errors.
pub trait ExtendWith<Item> {
type Error;
fn extend_with(&mut self, item: &Item) -> Result<(), Self::Error>;
}
FillToLenWithItems
Fills a collection with a list of items until it reaches a specified length.
pub trait FillToLenWithItems {
type Item;
fn fill_to_len(&mut self, len: usize, items: Vec<Self::Item>);
}
ReinitWithLen
Reinitializes a collection or structure to a specified length.
pub trait ReinitWithLen {
fn reinit(&mut self, len: usize);
}
FillWith
Fills a collection with a single value across all its elements.
pub trait FillWith {
type Item;
fn fill(&mut self, val: Self::Item);
}
InitInternals
Initializes internal data structures, returning a result to handle potential errors.
pub trait InitInternals {
type Error;
fn init_internals(&mut self) -> Result<(), Self::Error>;
}
InitWithSize
Initializes the internal structure with a given size.
pub trait InitWithSize {
fn init_size(&mut self, size: usize);
}
Clear
Clears all elements from the collection or structure.
pub trait Clear {
fn clear(&mut self);
}
CreateNamedEmpty
Creates an empty instance with an associated name.
pub trait CreateNamedEmpty {
fn empty(name: &str) -> Self;
}
CreateEmpty
Creates an empty instance of a collection or structure.
pub trait CreateEmpty {
fn empty() -> Self;
}
ResetWith
Resets the collection or structure using a provided input.
pub trait ResetWith<Input> {
fn reset_with(&mut self, g: &Input);
}
Add renew-traits to your Cargo.toml:
[dependencies]
renew-traits = "0.1"
Here's an example of how to implement some of the traits:
struct MyCollection {
data: Vec<i32>,
name: String,
}
impl CreateNamedEmpty for MyCollection {
fn empty(name: &str) -> Self {
Self { data: Vec::new(), name: name.to_string() }
}
}
impl ExtendWith<i32> for MyCollection {
type Error = ();
fn extend_with(&mut self, item: &i32) -> Result<(), Self::Error> {
self.data.push(*item);
Ok(())
}
}
This crate is licensed under either of:
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
Contributions are welcome! Feel free to submit a pull request or open an issue on GitHub.