Skip to content

Why so many traits? #27

@Kixunil

Description

@Kixunil

I'm wondering why there are so many traits in the crate. Traits are used for abstractions, but from what I've seen they don't abstract anything. Wouldn't it be simpler to not have them? Or, if abstraction is desirable, move the traits into separate crate and generalize them little bit more. This way they could be implemented for various databases. (So people who want to use other databases don't need to have leveldb in their dependencies.)

I imagine something like this:

trait KVDatabase {
    type BorrowedKey;
    /// Value that is returned from `get` method
    type OwnedValue: Borrow<Self::BorrowedValue>;
    /// Value that can be passed to `put` and `delete` methods
    type BorrowedValue;
    type GetError;
    type PutError;
    type DeleteError;

    fn get(&self, &Self::BorrowedKey) -> Result<Option<Self::OwnedValue>, Self::GetError>;
    fn put(&mut self, &Self::BorrowedKey, Self::BorrowedValue) -> Result<(), Self::PutError>;
    fn delete(&mut self, &Self::BorrowedKey) -> Result<bool, Self::DeleteError>;
}

trait IterKVDatabase: KVDatabase {
    type OwnedKey: Borrow<Self::BorrowedKey>;
    type KVIterator: Iterator<Item=(Self::OwnedKey, Self::OwnedValue)>;
    type KeyIterator: Iterator<Item=Self::OwnedKey>;
    type ValueIterator: Iterator<Item=Self::OwnedValue>;

   fn iter(&self) -> Self::KVIterator;
   fn iter_keys(&self) -> Self::KeyIterator;
   fn iter_values(&self) -> Self::ValueIterator;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions