heed-types-0.20.0-alpha.9/.cargo_vcs_info.json0000644000000001500000000000100144100ustar { "git": { "sha1": "aa56049fd947c13388e36be2d130a8828b6d0592" }, "path_in_vcs": "heed-types" }heed-types-0.20.0-alpha.9/Cargo.toml0000644000000027120000000000100124140ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "heed-types" version = "0.20.0-alpha.9" authors = ["Kerollmops "] description = "The types used with the fully typed LMDB wrapper, heed" readme = "README.md" license = "MIT" repository = "https://github.com/Kerollmops/heed" [dependencies.bincode] version = "1.3.3" optional = true [dependencies.byteorder] version = "1.4.3" [dependencies.heed-traits] version = "0.20.0-alpha.9" [dependencies.rmp-serde] version = "1.1.2" optional = true [dependencies.serde] version = "1.0.151" optional = true [dependencies.serde_json] version = "1.0.91" optional = true [features] arbitrary_precision = ["serde_json/arbitrary_precision"] default = [ "serde-bincode", "serde-json", ] preserve_order = ["serde_json/preserve_order"] raw_value = ["serde_json/raw_value"] serde-bincode = [ "serde", "bincode", ] serde-json = [ "serde", "serde_json", ] serde-rmp = [ "serde", "rmp-serde", ] unbounded_depth = ["serde_json/unbounded_depth"] heed-types-0.20.0-alpha.9/Cargo.toml.orig000064400000000000000000000017071046102023000161000ustar 00000000000000[package] name = "heed-types" version = "0.20.0-alpha.9" authors = ["Kerollmops "] description = "The types used with the fully typed LMDB wrapper, heed" license = "MIT" repository = "https://github.com/Kerollmops/heed" readme = "../README.md" edition = "2021" [dependencies] bincode = { version = "1.3.3", optional = true } byteorder = "1.4.3" heed-traits = { version = "0.20.0-alpha.9", path = "../heed-traits" } serde = { version = "1.0.151", optional = true } serde_json = { version = "1.0.91", optional = true } rmp-serde = { version = "1.1.2", optional = true } [features] default = ["serde-bincode", "serde-json"] serde-bincode = ["serde", "bincode"] serde-json = ["serde", "serde_json"] serde-rmp = ["serde", "rmp-serde"] # serde_json features preserve_order = ["serde_json/preserve_order"] arbitrary_precision = ["serde_json/arbitrary_precision"] raw_value = ["serde_json/raw_value"] unbounded_depth = ["serde_json/unbounded_depth"] heed-types-0.20.0-alpha.9/README.md000064400000000000000000000024151046102023000144650ustar 00000000000000

heed

[![License](https://img.shields.io/badge/license-MIT-green)](#LICENSE) [![Crates.io](https://img.shields.io/crates/v/heed)](https://crates.io/crates/heed) [![Docs](https://docs.rs/heed/badge.svg)](https://docs.rs/heed) [![dependency status](https://deps.rs/repo/github/meilisearch/heed/status.svg)](https://deps.rs/repo/github/meilisearch/heed) [![Build](https://github.com/meilisearch/heed/actions/workflows/rust.yml/badge.svg)](https://github.com/meilisearch/heed/actions/workflows/rust.yml) A fully typed [LMDB](https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Database) wrapper with minimum overhead, uses bytemuck internally. This library is able to serialize all kind of types, not just bytes slices, even _Serde_ types are supported. Go check out [the examples](heed/examples/). ## Building from Source If you don't already cloned the repository you can use this command: ```bash git clone --recursive https://github.com/meilisearch/heed.git cd heed cargo build ``` However, if you already cloned it and forgot to initialize the submodules execute the follwing command: ```bash git submodule update --init ``` heed-types-0.20.0-alpha.9/src/bytes.rs000064400000000000000000000011401046102023000154630ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes a slice of bytes `[u8]` that is totally /// borrowed and doesn't depends on any [memory alignment]. /// /// [memory alignment]: std::mem::align_of() pub enum Bytes {} impl<'a> BytesEncode<'a> for Bytes { type EItem = [u8]; fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { Ok(Cow::Borrowed(item)) } } impl<'a> BytesDecode<'a> for Bytes { type DItem = &'a [u8]; fn bytes_decode(bytes: &'a [u8]) -> Result { Ok(bytes) } } heed-types-0.20.0-alpha.9/src/decode_ignore.rs000064400000000000000000000006251046102023000171320ustar 00000000000000use heed_traits::BoxedError; /// A convenient struct made to ignore the type when decoding it. /// /// It is appropriate to be used to count keys for example /// or to ensure that an entry exist for example. pub enum DecodeIgnore {} impl heed_traits::BytesDecode<'_> for DecodeIgnore { type DItem = (); fn bytes_decode(_bytes: &[u8]) -> Result { Ok(()) } } heed-types-0.20.0-alpha.9/src/integer.rs000064400000000000000000000043071046102023000160020ustar 00000000000000use std::borrow::Cow; use std::marker::PhantomData; use std::mem::size_of; use byteorder::{ByteOrder, ReadBytesExt}; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Encodable version of [`u8`] pub struct U8; impl BytesEncode<'_> for U8 { type EItem = u8; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { Ok(Cow::from([*item].to_vec())) } } impl BytesDecode<'_> for U8 { type DItem = u8; fn bytes_decode(mut bytes: &'_ [u8]) -> Result { bytes.read_u8().map_err(Into::into) } } /// Encodable version of [`i8`] pub struct I8; impl BytesEncode<'_> for I8 { type EItem = i8; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { Ok(Cow::from([*item as u8].to_vec())) } } impl BytesDecode<'_> for I8 { type DItem = i8; fn bytes_decode(mut bytes: &'_ [u8]) -> Result { bytes.read_i8().map_err(Into::into) } } macro_rules! define_type { ($name:ident, $native:ident, $read_method:ident, $write_method:ident) => { #[doc = "Encodable version of [`"] #[doc = stringify!($native)] #[doc = "`]."] pub struct $name(PhantomData); impl BytesEncode<'_> for $name { type EItem = $native; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { let mut buf = vec![0; size_of::()]; O::$write_method(&mut buf, *item); Ok(Cow::from(buf)) } } impl BytesDecode<'_> for $name { type DItem = $native; fn bytes_decode(mut bytes: &'_ [u8]) -> Result { bytes.$read_method::().map_err(Into::into) } } }; } define_type!(U16, u16, read_u16, write_u16); define_type!(U32, u32, read_u32, write_u32); define_type!(U64, u64, read_u64, write_u64); define_type!(U128, u128, read_u128, write_u128); define_type!(I16, i16, read_i16, write_i16); define_type!(I32, i32, read_i32, write_i32); define_type!(I64, i64, read_i64, write_i64); define_type!(I128, i128, read_i128, write_i128); heed-types-0.20.0-alpha.9/src/lazy_decode.rs000064400000000000000000000020531046102023000166230ustar 00000000000000use std::marker; use heed_traits::BoxedError; /// Lazily decode the data bytes, it can be used to avoid CPU intensive decoding /// before making sure we really need to decode it (e.g. based on the key). #[derive(Default)] pub struct LazyDecode(marker::PhantomData); impl<'a, C: 'static> heed_traits::BytesDecode<'a> for LazyDecode { type DItem = Lazy<'a, C>; fn bytes_decode(bytes: &'a [u8]) -> Result { Ok(Lazy { data: bytes, _phantom: marker::PhantomData }) } } /// Owns bytes that can be decoded on demand. #[derive(Copy, Clone)] pub struct Lazy<'a, C> { data: &'a [u8], _phantom: marker::PhantomData, } impl<'a, C: heed_traits::BytesDecode<'a>> Lazy<'a, C> { /// Change the codec type of this database, specifying the new codec. pub fn remap(&self) -> Lazy<'a, NC> { Lazy { data: self.data, _phantom: marker::PhantomData } } /// Decode the given bytes as DItem pub fn decode(&self) -> Result { C::bytes_decode(self.data) } } heed-types-0.20.0-alpha.9/src/lib.rs000064400000000000000000000017341046102023000151140ustar 00000000000000#![doc( html_favicon_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon.ico?raw=true" )] #![doc( html_logo_url = "https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon-logo.png?raw=true" )] //! Types that can be used to serialize and deserialize types inside databases. #![warn(missing_docs)] mod bytes; mod decode_ignore; mod integer; mod lazy_decode; mod str; mod unit; #[cfg(feature = "serde-bincode")] mod serde_bincode; #[cfg(feature = "serde-json")] mod serde_json; #[cfg(feature = "serde-rmp")] mod serde_rmp; pub use self::bytes::Bytes; pub use self::decode_ignore::DecodeIgnore; pub use self::integer::*; pub use self::lazy_decode::{Lazy, LazyDecode}; #[cfg(feature = "serde-bincode")] pub use self::serde_bincode::SerdeBincode; #[cfg(feature = "serde-json")] pub use self::serde_json::SerdeJson; #[cfg(feature = "serde-rmp")] pub use self::serde_rmp::SerdeRmp; pub use self::str::Str; pub use self::unit::Unit; heed-types-0.20.0-alpha.9/src/serde_bincode.rs000064400000000000000000000016071046102023000171320ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `bincode` to do so. /// /// It can borrow bytes from the original slice. pub struct SerdeBincode(std::marker::PhantomData); impl<'a, T: 'a> BytesEncode<'a> for SerdeBincode where T: Serialize, { type EItem = T; fn bytes_encode(item: &'a Self::EItem) -> Result, BoxedError> { bincode::serialize(item).map(Cow::Owned).map_err(Into::into) } } impl<'a, T: 'a> BytesDecode<'a> for SerdeBincode where T: Deserialize<'a>, { type DItem = T; fn bytes_decode(bytes: &'a [u8]) -> Result { bincode::deserialize(bytes).map_err(Into::into) } } unsafe impl Send for SerdeBincode {} unsafe impl Sync for SerdeBincode {} heed-types-0.20.0-alpha.9/src/serde_json.rs000064400000000000000000000015721046102023000165010ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `serde_json` to do so. /// /// It can borrow bytes from the original slice. pub struct SerdeJson(std::marker::PhantomData); impl<'a, T: 'a> BytesEncode<'a> for SerdeJson where T: Serialize, { type EItem = T; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { serde_json::to_vec(item).map(Cow::Owned).map_err(Into::into) } } impl<'a, T: 'a> BytesDecode<'a> for SerdeJson where T: Deserialize<'a>, { type DItem = T; fn bytes_decode(bytes: &'a [u8]) -> Result { serde_json::from_slice(bytes).map_err(Into::into) } } unsafe impl Send for SerdeJson {} unsafe impl Sync for SerdeJson {} heed-types-0.20.0-alpha.9/src/serde_rmp.rs000064400000000000000000000015621046102023000163250ustar 00000000000000use std::borrow::Cow; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; use serde::{Deserialize, Serialize}; /// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `rmp_serde` to do so. /// /// It can borrow bytes from the original slice. pub struct SerdeRmp(std::marker::PhantomData); impl<'a, T: 'a> BytesEncode<'a> for SerdeRmp where T: Serialize, { type EItem = T; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { rmp_serde::to_vec(item).map(Cow::Owned).map_err(Into::into) } } impl<'a, T: 'a> BytesDecode<'a> for SerdeRmp where T: Deserialize<'a>, { type DItem = T; fn bytes_decode(bytes: &'a [u8]) -> Result { rmp_serde::from_slice(bytes).map_err(Into::into) } } unsafe impl Send for SerdeRmp {} unsafe impl Sync for SerdeRmp {} heed-types-0.20.0-alpha.9/src/str.rs000064400000000000000000000010061046102023000151460ustar 00000000000000use std::borrow::Cow; use std::str; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes an [`prim@str`]. pub enum Str {} impl BytesEncode<'_> for Str { type EItem = str; fn bytes_encode(item: &Self::EItem) -> Result, BoxedError> { Ok(Cow::Borrowed(item.as_bytes())) } } impl<'a> BytesDecode<'a> for Str { type DItem = &'a str; fn bytes_decode(bytes: &'a [u8]) -> Result { str::from_utf8(bytes).map_err(Into::into) } } heed-types-0.20.0-alpha.9/src/unit.rs000064400000000000000000000017001046102023000153160ustar 00000000000000use std::borrow::Cow; use std::{error, fmt}; use heed_traits::{BoxedError, BytesDecode, BytesEncode}; /// Describes the `()` type. pub enum Unit {} impl BytesEncode<'_> for Unit { type EItem = (); fn bytes_encode(_item: &Self::EItem) -> Result, BoxedError> { Ok(Cow::Borrowed(&[])) } } impl BytesDecode<'_> for Unit { type DItem = (); fn bytes_decode(bytes: &[u8]) -> Result { if bytes.is_empty() { Ok(()) } else { Err(NonEmptyError.into()) } } } /// The slice of bytes is non-empty and therefore is not a unit `()` type. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct NonEmptyError; impl fmt::Display for NonEmptyError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("the slice of bytes is non-empty and therefore is not a unit `()` type") } } impl error::Error for NonEmptyError {}