Struct bonsaidb_core::key::VarInt
source · 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:
u8
u16
u32
u64
u128
usize
i8
i16
i32
i64
i128
isize
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: T
Trait 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<VarInt<T>> for VarInt<T>where
T: VariableInteger,
impl<T> KeyEncoding<VarInt<T>> for VarInt<T>where T: VariableInteger,
source§const LENGTH: Option<usize> = None
const LENGTH: Option<usize> = None
None
.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> PartialEq<VarInt<T>> for VarInt<T>where
T: VariableInteger + PartialEq,
impl<T> PartialEq<VarInt<T>> for VarInt<T>where T: VariableInteger + PartialEq,
source§impl<T> PartialOrd<VarInt<T>> for VarInt<T>where
T: VariableInteger + PartialOrd,
impl<T> PartialOrd<VarInt<T>> for VarInt<T>where T: VariableInteger + PartialOrd,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more