pub struct VarInt<T>(pub T)
where
T: VariableInteger;Expand description
A wrapper type for Rust’s built-in integer types that encodes with variable
length and implements the Key trait.
This type supports all of Rust’s built-in integer types:
u8u16u32u64u128usizei8i16i32i64i128isize
use bonsaidb_core::key::{Key, KeyEncoding, VarInt};
#[derive(Key, Default, Clone)]
struct UserId(u64);
// `UserId` type will always encode to 8 bytes, since u64 will encode
// using `u64::to_be_bytes`.
let default_key_len = UserId::default().as_ord_bytes().unwrap().len();
assert_eq!(default_key_len, 8);
let another_key_len = UserId(u64::MAX).as_ord_bytes().unwrap().len();
assert_eq!(another_key_len, 8);
#[derive(Key, Default, Clone)]
struct UserIdVariable(VarInt<u64>);
// However, `UserIdVariable` will be able to encode in as little as 1 byte,
// but can take up to 9 bytes if the entire u64 range is utilized.
let default_key_len = UserIdVariable::default().as_ord_bytes().unwrap().len();
assert_eq!(default_key_len, 1);
let another_key_len = UserIdVariable(VarInt(u64::MAX))
.as_ord_bytes()
.unwrap()
.len();
assert_eq!(another_key_len, 9);§Why does this type exist?
The Key trait is implemented for all of Rust’s native integer types by
using to_be_bytes()/from_be_bytes(). This provides some benefits: very
fast encoding and decoding, and known-width encoding is faster to decode.
This type uses ordered_varint to encode the types using a variable
length encoding that is still compatible with the Key trait. This allows
a value of 0 to encode as a single byte while still preserving the correct
sort order required by Key.
Additionally, this encoding format allows for upgrading the in-memory size transparently if the value range needs increases over time. This only works between types that are signed the same.
§Behavior with Serde
This type implements serde::Serialize and serde::Deserialize
transparently, as many serialization formats implement native variable
integer encoding, and do not benefit from an ordered implementation.
Tuple Fields§
§0: TTrait Implementations§
Source§impl<T> Deref for VarInt<T>where
T: VariableInteger,
impl<T> Deref for VarInt<T>where
T: VariableInteger,
Source§impl<T> DerefMut for VarInt<T>where
T: VariableInteger,
impl<T> DerefMut for VarInt<T>where
T: VariableInteger,
Source§impl<'de, T> Deserialize<'de> for VarInt<T>where
T: Deserialize<'de> + VariableInteger,
impl<'de, T> Deserialize<'de> for VarInt<T>where
T: Deserialize<'de> + VariableInteger,
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T> From<T> for VarInt<T>where
T: VariableInteger,
impl<T> From<T> for VarInt<T>where
T: VariableInteger,
Source§impl<'k, T> Key<'k> for VarInt<T>where
T: VariableInteger,
impl<'k, T> Key<'k> for VarInt<T>where
T: VariableInteger,
Source§const CAN_OWN_BYTES: bool = false
const CAN_OWN_BYTES: bool = false
Vec<u8>. This flag is
used as a hint of whether to attempt to do memcpy operations in some
decoding operations to avoid extra allocations.Source§fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error>
fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error>
KeyEncoding::as_ord_bytes.Source§fn first_value() -> Result<Self, NextValueError>
fn first_value() -> Result<Self, NextValueError>
Source§fn next_value(&self) -> Result<Self, NextValueError>
fn next_value(&self) -> Result<Self, NextValueError>
Source§impl<T> KeyEncoding for VarInt<T>where
T: VariableInteger,
impl<T> KeyEncoding for VarInt<T>where
T: VariableInteger,
Source§const LENGTH: Option<usize> = None
const LENGTH: Option<usize> = None
None.Source§type Error = Error
type Error = Error
Source§fn describe<Visitor>(visitor: &mut Visitor)where
Visitor: KeyVisitor,
fn describe<Visitor>(visitor: &mut Visitor)where
Visitor: KeyVisitor,
visitor describing the
key being encoded. Read moreSource§impl<T> Ord for VarInt<T>where
T: VariableInteger + Ord,
impl<T> Ord for VarInt<T>where
T: VariableInteger + Ord,
Source§impl<T> PartialOrd for VarInt<T>where
T: VariableInteger + PartialOrd,
impl<T> PartialOrd for VarInt<T>where
T: VariableInteger + PartialOrd,
impl<T> Copy for VarInt<T>where
T: VariableInteger + Copy,
impl<T> Eq for VarInt<T>where
T: VariableInteger + Eq,
impl<T> StructuralPartialEq for VarInt<T>where
T: VariableInteger,
Auto Trait Implementations§
impl<T> Freeze for VarInt<T>where
T: Freeze,
impl<T> RefUnwindSafe for VarInt<T>where
T: RefUnwindSafe,
impl<T> Send for VarInt<T>
impl<T> Sync for VarInt<T>
impl<T> Unpin for VarInt<T>where
T: Unpin,
impl<T> UnwindSafe for VarInt<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more