mintlayer-core icon indicating copy to clipboard operation
mintlayer-core copied to clipboard

Replace Cow with an associated type in ReadOps::get() method in storage abstraction

Open TheQuantumPhysicist opened this issue 2 years ago • 0 comments
trafficstars

The get() method used to return a slice &[u8], but that's very difficult to guarantee to work for all libraries. Usually key-value stores offer such a nice interface where the returned value within a transaction can be a slice directly read from the database. However, SQL data cannot do that. Hence, we need to return things by value for them. This is why #652 happened. We now return a Cow object.

A better way to solve this problem would be by using an associated type in ReadOps, where the return type is defined in the trait's implementation. Something in the form:

pub trait ReadOps: for<'i> PrefixIter<'i> {
    type DataType: AsRef<[u8]>;
    /// Get value associated with given key.
    fn get(&self, idx: DbIndex, key: &[u8]) -> crate::Result<Option<Self::DataType>>;
}

TheQuantumPhysicist avatar Jan 28 '23 09:01 TheQuantumPhysicist