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<VarInt<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<VarInt<T>, <D as Deserializer<'de>>::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<VarInt<T>, <VarInt<T> as KeyEncoding>::Error>
fn from_ord_bytes<'e>( bytes: ByteSource<'k, 'e> ) -> Result<VarInt<T>, <VarInt<T> as KeyEncoding>::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§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§fn as_ord_bytes(
&self
) -> Result<Cow<'_, [u8]>, <VarInt<T> as KeyEncoding>::Error>
fn as_ord_bytes( &self ) -> Result<Cow<'_, [u8]>, <VarInt<T> as KeyEncoding>::Error>
self
into a Cow<'_, [u8]>
containing bytes that are able to be
compared via memcmp
in a way that is comptaible with its own Ord
implementation.source§impl<T> Ord for VarInt<T>where
T: Ord + VariableInteger,
impl<T> Ord for VarInt<T>where
T: Ord + VariableInteger,
source§impl<T> PartialEq for VarInt<T>where
T: PartialEq + VariableInteger,
impl<T> PartialEq for VarInt<T>where
T: PartialEq + VariableInteger,
source§impl<T> PartialOrd for VarInt<T>where
T: PartialOrd + VariableInteger,
impl<T> PartialOrd for VarInt<T>where
T: PartialOrd + VariableInteger,
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 moresource§impl<T> Serialize for VarInt<T>where
T: Serialize + VariableInteger,
impl<T> Serialize for VarInt<T>where
T: Serialize + VariableInteger,
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<T> Copy for VarInt<T>where
T: Copy + VariableInteger,
impl<T> Eq for VarInt<T>where
T: Eq + VariableInteger,
impl<T> StructuralEq for VarInt<T>where
T: VariableInteger,
impl<T> StructuralPartialEq for VarInt<T>where
T: VariableInteger,
Auto Trait Implementations§
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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
§impl<T> CallHasher for T
impl<T> CallHasher for T
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.