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§
§impl<T> Add<T> for VarInt<T>where
T: Add<T, Output = T> + VariableInteger,
impl<T> Add<T> for VarInt<T>where T: Add<T, Output = T> + VariableInteger,
§impl<T> BitAnd<T> for VarInt<T>where
T: BitAnd<T, Output = T> + VariableInteger,
impl<T> BitAnd<T> for VarInt<T>where T: BitAnd<T, Output = T> + VariableInteger,
§impl<T> BitOr<T> for VarInt<T>where
T: BitOr<T, Output = T> + VariableInteger,
impl<T> BitOr<T> for VarInt<T>where T: BitOr<T, Output = T> + VariableInteger,
§impl<T> BitXor<T> for VarInt<T>where
T: BitXor<T, Output = T> + VariableInteger,
impl<T> BitXor<T> for VarInt<T>where T: BitXor<T, Output = T> + VariableInteger,
§impl<T> Clone for VarInt<T>where
T: Clone + VariableInteger,
impl<T> Clone for VarInt<T>where T: Clone + VariableInteger,
§impl<T> Debug for VarInt<T>where
T: Debug + VariableInteger,
impl<T> Debug for VarInt<T>where T: Debug + VariableInteger,
§impl<T> Default for VarInt<T>where
T: Default + VariableInteger,
impl<T> Default for VarInt<T>where T: Default + VariableInteger,
§impl<T> Deref for VarInt<T>where
T: VariableInteger,
impl<T> Deref for VarInt<T>where T: VariableInteger,
§impl<T> DerefMut for VarInt<T>where
T: VariableInteger,
impl<T> DerefMut for VarInt<T>where T: VariableInteger,
§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,
§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>,
§impl<T> Display for VarInt<T>where
T: Display + VariableInteger,
impl<T> Display for VarInt<T>where T: Display + VariableInteger,
§impl<T> Div<T> for VarInt<T>where
T: Div<T, Output = T> + VariableInteger,
impl<T> Div<T> for VarInt<T>where T: Div<T, Output = T> + VariableInteger,
§impl<T> From<T> for VarInt<T>where
T: VariableInteger,
impl<T> From<T> for VarInt<T>where T: VariableInteger,
§impl<T> Hash for VarInt<T>where
T: Hash + VariableInteger,
impl<T> Hash for VarInt<T>where T: Hash + VariableInteger,
§impl<'k, T> Key<'k> for VarInt<T>where
T: VariableInteger,
impl<'k, T> Key<'k> for VarInt<T>where T: VariableInteger,
§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.§fn from_ord_bytes<'e>(
bytes: ByteSource<'k, 'e>
) -> Result<VarInt<T>, <VarInt<T> as KeyEncoding<VarInt<T>>>::Error>
fn from_ord_bytes<'e>( bytes: ByteSource<'k, 'e> ) -> Result<VarInt<T>, <VarInt<T> as KeyEncoding<VarInt<T>>>::Error>
KeyEncoding::as_ord_bytes
.§fn first_value() -> Result<Self, NextValueError>
fn first_value() -> Result<Self, NextValueError>
§fn next_value(&self) -> Result<Self, NextValueError>
fn next_value(&self) -> Result<Self, NextValueError>
§impl<T> KeyEncoding<VarInt<T>> for VarInt<T>where
T: VariableInteger,
impl<T> KeyEncoding<VarInt<T>> for VarInt<T>where T: VariableInteger,
§const LENGTH: Option<usize> = None
const LENGTH: Option<usize> = None
None
.§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 more§fn as_ord_bytes(
&self
) -> Result<Cow<'_, [u8]>, <VarInt<T> as KeyEncoding<VarInt<T>>>::Error>
fn as_ord_bytes( &self ) -> Result<Cow<'_, [u8]>, <VarInt<T> as KeyEncoding<VarInt<T>>>::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.§impl<T> Mul<T> for VarInt<T>where
T: Mul<T, Output = T> + VariableInteger,
impl<T> Mul<T> for VarInt<T>where T: Mul<T, Output = T> + VariableInteger,
§impl<T> Not for VarInt<T>where
T: Not<Output = T> + VariableInteger,
impl<T> Not for VarInt<T>where T: Not<Output = T> + VariableInteger,
§impl<T> Ord for VarInt<T>where
T: Ord + VariableInteger,
impl<T> Ord for VarInt<T>where T: Ord + VariableInteger,
§impl<T> PartialOrd<VarInt<T>> for VarInt<T>where
T: PartialOrd<T> + VariableInteger,
impl<T> PartialOrd<VarInt<T>> for VarInt<T>where T: PartialOrd<T> + VariableInteger,
§fn partial_cmp(&self, other: &VarInt<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &VarInt<T>) -> Option<Ordering>
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§impl<T> Rem<T> for VarInt<T>where
T: Rem<T, Output = T> + VariableInteger,
impl<T> Rem<T> for VarInt<T>where T: Rem<T, Output = T> + VariableInteger,
§impl<T> Serialize for VarInt<T>where
T: Serialize + VariableInteger,
impl<T> Serialize for VarInt<T>where T: Serialize + VariableInteger,
§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> Shl<T> for VarInt<T>where
T: Shl<T, Output = T> + VariableInteger,
impl<T> Shl<T> for VarInt<T>where T: Shl<T, Output = T> + VariableInteger,
§impl<T> Shr<T> for VarInt<T>where
T: Shr<T, Output = T> + VariableInteger,
impl<T> Shr<T> for VarInt<T>where T: Shr<T, Output = T> + VariableInteger,
§impl<T> Sub<T> for VarInt<T>where
T: Sub<T, Output = T> + VariableInteger,
impl<T> Sub<T> for VarInt<T>where T: Sub<T, Output = T> + VariableInteger,
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
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.