1
/// [`Key`] implementations for time types.
2
pub mod time;
3
mod varint;
4

            
5
mod deprecated;
6

            
7
use std::any::type_name;
8
use std::borrow::{Borrow, Cow};
9
use std::collections::HashMap;
10
use std::convert::Infallible;
11
use std::io::{self, ErrorKind};
12
use std::num::{
13
    NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
14
    NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, TryFromIntError,
15
};
16
use std::ops::Deref;
17
use std::string::FromUtf8Error;
18

            
19
use arc_bytes::serde::{Bytes, CowBytes};
20
use arc_bytes::ArcBytes;
21
pub use bonsaidb_macros::Key;
22
pub use deprecated::*;
23
use num_traits::{FromPrimitive, ToPrimitive};
24
use ordered_varint::{Signed, Unsigned, Variable};
25
use serde::{Deserialize, Serialize};
26
use transmog::BorrowedDeserializer;
27
pub use varint::{VarInt, VariableInteger};
28

            
29
use crate::connection::{Bound, BoundRef, MaybeOwned, RangeRef};
30
use crate::AnyError;
31

            
32
/// A trait that enables a type to convert itself into a `memcmp`-compatible
33
/// sequence of bytes.
34
pub trait KeyEncoding<K = Self>: Send + Sync {
35
    /// The error type that can be produced by either serialization or
36
    /// deserialization.
37
    type Error: AnyError;
38

            
39
    /// The size of the key, if constant. If this type doesn't produce the same
40
    /// number of bytes for each value, this should be `None`.
41
    const LENGTH: Option<usize>;
42

            
43
    /// Describes this type by invoking functions on `visitor` describing the
44
    /// key being encoded.
45
    ///
46
    /// See the [`KeyVisitor`] trait for more information.
47
    ///
48
    /// [`KeyDescription::for_key()`]/[`KeyDescription::for_encoding()`] are
49
    /// built-in functions
50
    fn describe<Visitor>(visitor: &mut Visitor)
51
    where
52
        Visitor: KeyVisitor;
53

            
54
    /// Convert `self` into a `Cow<'_, [u8]>` containing bytes that are able to be
55
    /// compared via `memcmp` in a way that is comptaible with its own Ord
56
    /// implementation.
57
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error>;
58
}
59

            
60
/// A trait that enables a type to convert itself into a `memcmp`-compatible
61
/// sequence of bytes.
62
///
63
/// # Deriving this trait
64
///
65
/// This trait can be derived on structs and enums whose members all implement
66
/// `Key`. It is important to note that the order of individual fields and enum
67
/// variants is important, so special care must be taken when updating enums and
68
/// structs when trying to preserve backwards compatibility with existing data.
69
///
70
/// ```rust
71
/// use bonsaidb_core::key::Key;
72
///
73
/// #[derive(Key, Clone, Debug)]
74
/// # #[key(core = bonsaidb_core)]
75
/// struct CompositeKey {
76
///     user_id: u64,
77
///     task_id: u32,
78
/// }
79
/// ```
80
///
81
/// Each field or enum variant is encoded and decoded in the order in which it
82
/// appears in the source code. The implementation uses [`CompositeKeyEncoder`]
83
/// and [`CompositeKeyDecoder`] to encode each field.
84
///
85
/// ## Changing the `enum` representation type
86
///
87
/// By default, the derived `Key` implementation will use an `isize` for its
88
/// representation, which is encoded using [`ordered_varint`]. If you wish to
89
/// use a fixed-size encoding or use `usize`, `enum_repr` can be used to control
90
/// the type being encoded.
91
///
92
/// The default behavior produces compact output for simple enums, but can also
93
/// support growing to the limits of `isize`:
94
///
95
/// ```rust
96
/// use bonsaidb_core::key::{Key, KeyEncoding};
97
///
98
/// #[derive(Key, Clone, Debug)]
99
/// # #[key(core = bonsaidb_core)]
100
/// enum Color {
101
///     Red,
102
///     Green,
103
///     Blue,
104
/// }
105
///
106
/// let encoded = Color::Red.as_ord_bytes().unwrap();
107
/// assert_eq!(encoded.len(), 1);
108
/// ```
109
///
110
/// If a `#[repr(...)]` attribute exists and its parameter is a built-in integer
111
/// type, the `Key` derive will use that type for its representation instead:
112
///
113
/// ```rust
114
/// use bonsaidb_core::key::{Key, KeyEncoding};
115
///
116
/// #[derive(Key, Clone, Debug)]
117
/// # #[key(core = bonsaidb_core)]
118
/// #[repr(u32)]
119
/// enum Color {
120
///     Red = 0xFF0000FF,
121
///     Green = 0x00FF00FF,
122
///     Blue = 0x0000FFFF,
123
/// }
124
///
125
/// let encoded = Color::Red.as_ord_bytes().unwrap();
126
/// assert_eq!(encoded.len(), 4);
127
/// ```
128
///
129
/// If the type would rather use a different type for the key encoding than it
130
/// uses for in-memory representation, the `enum_repr` parameter can be used:
131
///
132
/// ```rust
133
/// use bonsaidb_core::key::{Key, KeyEncoding};
134
///
135
/// #[derive(Key, Clone, Debug)]
136
/// # #[key(core = bonsaidb_core)]
137
/// #[key(enum_repr = u32)]
138
/// #[repr(usize)]
139
/// enum Color {
140
///     Red = 0xFF0000FF,
141
///     Green = 0x00FF00FF,
142
///     Blue = 0x0000FFFF,
143
/// }
144
///
145
/// let encoded = Color::Red.as_ord_bytes().unwrap();
146
/// assert_eq!(encoded.len(), 4);
147
/// ```
148
///
149
/// ## `null_handling`
150
///
151
/// The derive macro offers an argument `null_handling`, which defaults to
152
/// `escape`. Escaping null bytes in variable length fields ensures all data
153
/// encoded will sort correctly. In situations where speed is more important
154
/// than sorting behavior, `allow` or `deny` can be specified. `allow` will
155
/// permit null bytes to pass through encoding and decoding without being
156
/// checked. `deny` will check for null bytes when encoding and return an error
157
/// if any are present.
158
///
159
/// Null bytes can cause problematic sort behavior when multiple variable-length
160
/// encoded fields are encoded as a composite key. Consider this example:
161
///
162
/// ```rust
163
/// use bonsaidb_core::key::Key;
164
///
165
/// #[derive(Key, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
166
/// #[key(null_handling = allow)]
167
/// # #[key(core = bonsaidb_core)]
168
/// struct CompositeKey {
169
///     a: String,
170
///     b: String,
171
/// }
172
/// ```
173
///
174
/// With this structure, we can cause sorting misbehaviors using this data set:
175
///
176
/// | `a`      | `b`   | Encoded Bytes         |
177
/// |----------|-------|-----------------------|
178
/// | `"a"`    | `"c"` | `6100 6300 0101`      |
179
/// | `"a\0b"` | `"a"` | `6100 6200 610003 01` |
180
/// | `"b"`    | `"a"` | `6200 6100 0101`      |
181
///
182
/// In this table, `a` and `b` are ordered as `CompositeKey` would be ordered
183
/// when compared using `Ord`. However, the order of the encoded bytes does not
184
/// match.
185
///
186
/// This null-byte edge case only applies to variable length [`Key`]s
187
/// ([`KeyEncoding::LENGTH`] is `None`).
188
pub trait Key<'k>: KeyEncoding<Self> + Clone + Send + Sync {
189
    /// If true, this type can benefit from an owned `Vec<u8>`. This flag is
190
    /// used as a hint of whether to attempt to do memcpy operations in some
191
    /// decoding operations to avoid extra allocations.
192
    const CAN_OWN_BYTES: bool;
193

            
194
    /// Deserialize a sequence of bytes previously encoded with
195
    /// [`KeyEncoding::as_ord_bytes`].
196
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error>;
197

            
198
    /// Return the first value in sequence for this type. Not all types
199
    /// implement this.
200
    fn first_value() -> Result<Self, NextValueError> {
201
        Err(NextValueError::Unsupported)
202
    }
203

            
204
    /// Return the next value in sequence for this type. Not all types implement
205
    /// this. Instead of wrapping/overflowing, None should be returned.
206
1
    fn next_value(&self) -> Result<Self, NextValueError> {
207
1
        Err(NextValueError::Unsupported)
208
1
    }
209
}
210

            
211
impl<'a, 'k, K, KE> KeyEncoding<K> for &'a KE
212
where
213
    KE: KeyEncoding<K> + ?Sized + PartialEq,
214
    K: Key<'k> + PartialEq<KE>,
215
{
216
    type Error = KE::Error;
217

            
218
    const LENGTH: Option<usize> = K::LENGTH;
219

            
220
    fn describe<Visitor>(visitor: &mut Visitor)
221
    where
222
        Visitor: KeyVisitor,
223
    {
224
        KE::describe(visitor);
225
    }
226

            
227
211
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
228
211
        (*self).as_ord_bytes()
229
211
    }
230
}
231

            
232
/// A Visitor for information about a [`KeyEncoding`].
233
///
234
/// This trait is used in [`KeyEncoding::describe`] to report information about
235
/// the type of data contained within the key. Using this information,
236
/// generically interpreting the bytes of a key type can be possible as long as
237
/// the key type uses [`CompositeKeyEncoder`].
238
///
239
/// This trait is not something that most users will ever need to implement.
240
/// Instead,
241
/// [`KeyDescription::for_key()`]/[`KeyDescription::for_encoding()`] are
242
/// built-in functions to retrieve the information reported by this trait in an
243
/// easier-to-use interface.
244
pub trait KeyVisitor {
245
    /// Report that a basic key type is encoded next in this key.
246
    fn visit_type(&mut self, kind: KeyKind);
247

            
248
    /// Report that a custom type is encoded next as a single byte sequence in
249
    /// this key.
250
    fn visit_other(&mut self, kind: impl Into<Cow<'static, str>>);
251

            
252
    /// Report that a composite type made up of `count` fields is encoded next in
253
    /// this key.
254
    ///
255
    /// `KeyVisitor` implementations may panic if there are less than or more
256
    /// than `count` fields reported by either `visit_type()` or
257
    /// `visit_composite()`.
258
    fn visit_composite(&mut self, kind: CompositeKind, count: usize);
259

            
260
    /// Report that the current composite type has extra metadata.
261
    ///
262
    /// This can be used to encode const generic parameters that are helpful in
263
    /// interpretting the contents of a type. For example,
264
    /// [`LimitedResolutionTimestamp`](time::limited::LimitedResolutionTimestamp)
265
    /// uses this function to report the `TimeEpoch`'s nanoseconds since the
266
    /// Unix epoch.
267
    fn visit_composite_attribute(
268
        &mut self,
269
        key: impl Into<Cow<'static, str>>,
270
        value: impl Into<KeyAttibuteValue>,
271
    );
272
}
273

            
274
/// The type of a single field contained in a key.
275
5585
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
276
pub enum KeyKind {
277
    /// An `unit` type, encoded with no length.
278
    Unit,
279
    /// An `u8` encoded in big-endian encoding.
280
    U8,
281
    /// An `u16` encoded in big-endian encoding.
282
    U16,
283
    /// An `u32` encoded in big-endian encoding.
284
    U32,
285
    /// An `u64` encoded in big-endian encoding.
286
    U64,
287
    /// An `u128` encoded in big-endian encoding.
288
    U128,
289
    /// An `usize` encoded in big-endian encoding.
290
    Usize,
291
    /// An `i8` encoded in big-endian encoding.
292
    I8,
293
    /// An `i16` encoded in big-endian encoding.
294
    I16,
295
    /// An `i32` encoded in big-endian encoding.
296
    I32,
297
    /// An `i64` encoded in big-endian encoding.
298
    I64,
299
    /// An `i128` encoded in big-endian encoding.
300
    I128,
301
    /// An `isize` encoded in big-endian encoding.
302
    Isize,
303
    /// A [`Signed`] number encoded using [`ordered_varint`].
304
    Signed,
305
    /// An [`Unsigned`] number encoded using [`ordered_varint`].
306
    Unsigned,
307
    /// A `bool` encoded as a single byte.
308
    Bool,
309
    /// A String encoded using BonsaiDb's built-in `KeyEncoding`.
310
    String,
311
    /// A byte array encoded using BonsaiDb's built-in `KeyEncoding`.
312
    Bytes,
313
}
314

            
315
/// A value used as part of [`KeyVisitor::visit_composite_attribute`].
316
1
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
317
pub enum KeyAttibuteValue {
318
    /// An `u8` value.
319
    U8(u8),
320
    /// An `i8` value.
321
    I8(i8),
322
    /// An `u16` value.
323
    U16(u16),
324
    /// An `i16` value.
325
    I16(i16),
326
    /// An `u32` value.
327
    U32(u32),
328
    /// An `i32` value.
329
    I32(i32),
330
    /// An `u64` value.
331
    U64(u64),
332
    /// An `i64` value.
333
    I64(i64),
334
    /// An `u128` value.
335
    U128(u128),
336
    /// An `i128` value.
337
    I128(i128),
338
    /// An `usize` value.
339
    Usize(usize),
340
    /// An `isize` value.
341
    Isize(isize),
342
    /// A `bool` value.
343
    Bool(bool),
344
    /// A string value.
345
    Str(Cow<'static, str>),
346
    /// A byte array value.
347
    Bytes(Cow<'static, [u8]>),
348
}
349

            
350
macro_rules! impl_const_key_from {
351
    ($from:ty, $constkey:expr) => {
352
        impl From<$from> for KeyAttibuteValue {
353
1
            fn from(value: $from) -> Self {
354
1
                #[allow(clippy::redundant_closure_call)]
355
1
                $constkey(value)
356
1
            }
357
        }
358
    };
359
}
360

            
361
impl_const_key_from!(u8, KeyAttibuteValue::U8);
362
impl_const_key_from!(i8, KeyAttibuteValue::I8);
363
impl_const_key_from!(u16, KeyAttibuteValue::U16);
364
impl_const_key_from!(i16, KeyAttibuteValue::I16);
365
impl_const_key_from!(u32, KeyAttibuteValue::U32);
366
impl_const_key_from!(i32, KeyAttibuteValue::I32);
367
impl_const_key_from!(u64, KeyAttibuteValue::U64);
368
impl_const_key_from!(i64, KeyAttibuteValue::I64);
369
impl_const_key_from!(u128, KeyAttibuteValue::U128);
370
impl_const_key_from!(i128, KeyAttibuteValue::I128);
371
impl_const_key_from!(usize, KeyAttibuteValue::Usize);
372
impl_const_key_from!(isize, KeyAttibuteValue::Isize);
373
impl_const_key_from!(bool, KeyAttibuteValue::Bool);
374
impl_const_key_from!(&'static str, |s: &'static str| KeyAttibuteValue::Str(
375
    Cow::Borrowed(s)
376
));
377
impl_const_key_from!(String, |s: String| KeyAttibuteValue::Str(Cow::Owned(s)));
378
impl_const_key_from!(&'static [u8], |b: &'static [u8]| KeyAttibuteValue::Bytes(
379
    Cow::Borrowed(b)
380
));
381
impl_const_key_from!(Vec<u8>, |b: Vec<u8>| KeyAttibuteValue::Bytes(Cow::Owned(b)));
382

            
383
/// A description of the kind of a composite key.
384
542
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
385
pub enum CompositeKind {
386
    /// An [`Option`], which always contains a single field.
387
    Option,
388
    /// An [`Result`], which reports two fields -- the first for the `Ok` type,
389
    /// and the second for the `Err` type.
390
    Result,
391
    /// A sequence of fields.
392
    Tuple,
393
    /// A sequence of fields, identified by the fully qualified name. E.g.,
394
    /// `"std::time::SystemTime"` is the value that the
395
    /// [`SystemTime`](std::time::SystemTime)'s `KeyEncoding` implementation
396
    /// reports.
397
    Struct(Cow<'static, str>),
398
}
399

            
400
/// A description of an encoded [`Key`].
401
6128
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
402
pub enum KeyDescription {
403
    /// A basic key type.
404
    Basic(KeyKind),
405
    /// A composite key made up of more than one field.
406
    Composite(CompositeKeyDescription),
407
    /// Another type with a custom encoding format.
408
    Other(Cow<'static, str>),
409
}
410

            
411
impl KeyDescription {
412
    /// Returns a description of the given [`KeyEncoding`] implementor.
413
    ///
414
    /// # Panics
415
    ///
416
    /// This function will panic if `KE` emits an imbalanced sequence of visit
417
    /// calls.
418
    #[must_use]
419
5838181
    pub fn for_encoding<KE: KeyEncoding<K>, K: for<'k> Key<'k> + 'static>() -> Self {
420
5838181
        let mut describer = KeyDescriber::default();
421
5838181
        KE::describe(&mut describer);
422
5838181
        describer.finish_for::<K>()
423
5838181
    }
424

            
425
    /// Returns the description of a given [`Key`] implementor.
426
    ///
427
    /// # Panics
428
    ///
429
    /// This function will panic if `KE` emits an imbalanced sequence of visit
430
    /// calls.
431
    #[must_use]
432
5838181
    pub fn for_key<K: for<'k> Key<'k> + 'static>() -> Self {
433
5838181
        Self::for_encoding::<K, K>()
434
5838181
    }
435
}
436

            
437
/// A description of a multi-field key encoded using [`CompositeKeyEncoder`].
438
1892
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
439
pub struct CompositeKeyDescription {
440
    /// The kind of composite key.
441
    pub kind: CompositeKind,
442
    /// The fields contained within this key.
443
    pub fields: Vec<KeyDescription>,
444
    /// The attributes of this key.
445
    pub attributes: HashMap<Cow<'static, str>, KeyAttibuteValue>,
446
}
447

            
448
5838181
#[derive(Default)]
449
struct KeyDescriber {
450
    stack: Vec<CompositeKeyDescription>,
451
    result: Option<KeyDescription>,
452
}
453

            
454
impl KeyDescriber {
455
12716408
    fn record(&mut self, description: KeyDescription) {
456
12716408
        match self.stack.last_mut() {
457
1124
            Some(composite) => {
458
1124
                composite.fields.push(description);
459
1124
                if composite.fields.len() == composite.fields.capacity() {
460
1042
                    // The composite key is finished, pop the state.
461
1042
                    let completed = self.stack.pop().expect("just matched");
462
1042
                    self.record(KeyDescription::Composite(completed));
463
1042
                }
464
            }
465
            None => {
466
                // Stack has been completed.
467
12715284
                assert!(self.result.replace(description).is_none());
468
            }
469
        }
470
12716408
    }
471

            
472
5838181
    fn finish_for<T>(self) -> KeyDescription
473
5838181
    where
474
5838181
        T: 'static,
475
5838181
    {
476
5838181
        assert!(
477
5838181
            self.stack.is_empty(),
478
            "invalid KeyEncoding::describe implementation -- imbalanced visit calls"
479
        );
480
5838181
        self.result
481
5838181
            .unwrap_or_else(|| KeyDescription::Other(Cow::Borrowed(type_name::<T>())))
482
5838181
    }
483
}
484

            
485
impl KeyVisitor for KeyDescriber {
486
12715366
    fn visit_type(&mut self, kind: KeyKind) {
487
12715366
        let description = KeyDescription::Basic(kind);
488
12715366
        self.record(description);
489
12715366
    }
490

            
491
    fn visit_other(&mut self, kind: impl Into<Cow<'static, str>>) {
492
        self.record(KeyDescription::Other(kind.into()));
493
    }
494

            
495
1042
    fn visit_composite(&mut self, kind: CompositeKind, count: usize) {
496
1042
        self.stack.push(CompositeKeyDescription {
497
1042
            kind,
498
1042
            fields: Vec::with_capacity(count),
499
1042
            attributes: HashMap::new(),
500
1042
        });
501
1042
    }
502

            
503
1
    fn visit_composite_attribute(
504
1
        &mut self,
505
1
        key: impl Into<Cow<'static, str>>,
506
1
        value: impl Into<KeyAttibuteValue>,
507
1
    ) {
508
1
        let current = self
509
1
            .stack
510
1
            .last_mut()
511
1
            .expect("visit_composite_attribute must be called only after visit_composite");
512
1
        current.attributes.insert(key.into(), value.into());
513
1
    }
514
}
515

            
516
/// The error types for [`Key::next_value()`].
517
#[derive(Clone, thiserror::Error, Debug, Serialize, Deserialize)]
518
pub enum NextValueError {
519
    /// The key type does not support this operation.
520
    #[error("the key type does not support automatic ids")]
521
    Unsupported,
522
    /// Generating a new value would wrap the underlying value.
523
    #[error("the key type has run out of unique values")]
524
    WouldWrap,
525
}
526

            
527
/// A source of bytes from a `&'borrowed [u8]`, `&'ephemeral [u8]` or `Vec<u8>`.
528
///
529
/// This is used by the [`Key`] trait to allow `'ephemeral` borrows of an owned
530
/// `Vec<u8>` while also allowing for code paths that support borrowing from a
531
/// `'borrowed` byte slice.
532
pub enum ByteSource<'borrowed, 'ephemeral> {
533
    /// Borrowed bytes valid for `'borrowed`.
534
    Borrowed(&'borrowed [u8]),
535

            
536
    /// Borrowed bytes valid for `'ephemeral`.
537
    Ephemeral(&'ephemeral [u8]),
538

            
539
    /// Owned bytes.
540
    Owned(Vec<u8>),
541
}
542

            
543
impl<'borrowed, 'ephemeral> ByteSource<'borrowed, 'ephemeral>
544
where
545
    'borrowed: 'ephemeral,
546
{
547
    /// Coerce a `ByteSource` into `Cow<'borrowed, [u8]>`
548
    ///
549
    /// This will allocate if this is an `'ephemeral` reference.
550
    #[must_use]
551
124
    pub fn into_borrowed(self) -> Cow<'borrowed, [u8]> {
552
124
        match self {
553
123
            Self::Borrowed(bytes) => Cow::Borrowed(bytes),
554
            Self::Ephemeral(bytes) => Cow::Owned(Vec::from(bytes)),
555
1
            Self::Owned(bytes) => Cow::Owned(bytes),
556
        }
557
124
    }
558

            
559
    /// Coerce a `ByteSource` into a `Vec<u8>`
560
    ///
561
    /// This will allocate if the source is `'borrowed` or `'ephemeral`.
562
    #[must_use]
563
    #[allow(clippy::match_same_arms)] // Lifetimes are different.
564
552819
    pub fn into_owned(self) -> Vec<u8> {
565
552819
        match self {
566
552819
            Self::Borrowed(bytes) => Vec::from(bytes),
567
            Self::Ephemeral(bytes) => Vec::from(bytes),
568
            Self::Owned(bytes) => bytes,
569
        }
570
552819
    }
571
}
572

            
573
impl<'borrowed, 'ephemeral> AsRef<[u8]> for ByteSource<'borrowed, 'ephemeral> {
574
    #[allow(clippy::match_same_arms)] // Lifetimes are different.
575
326225994
    fn as_ref(&self) -> &[u8] {
576
326225994
        match self {
577
326225994
            Self::Borrowed(bytes) => bytes,
578
            Self::Ephemeral(bytes) => bytes,
579
            Self::Owned(ref bytes) => bytes.as_slice(),
580
        }
581
326225994
    }
582
}
583

            
584
impl<'borrowed, 'ephemeral> Deref for ByteSource<'borrowed, 'ephemeral> {
585
    type Target = [u8];
586

            
587
5696
    fn deref(&self) -> &Self::Target {
588
5696
        self.as_ref()
589
5696
    }
590
}
591

            
592
impl<'borrowed, 'ephemeral> Default for ByteSource<'borrowed, 'ephemeral> {
593
    fn default() -> Self {
594
        Self::Owned(Vec::default())
595
    }
596
}
597

            
598
impl<'borrowed, 'ephemeral> From<Cow<'borrowed, [u8]>> for ByteSource<'borrowed, 'ephemeral> {
599
    fn from(cow: Cow<'borrowed, [u8]>) -> Self {
600
        match cow {
601
            Cow::Borrowed(bytes) => ByteSource::Borrowed(bytes),
602
            Cow::Owned(bytes) => ByteSource::Owned(bytes),
603
        }
604
    }
605
}
606

            
607
/// A type that can be used as a prefix range in range-based queries.
608
pub trait IntoPrefixRange<'a, TOwned>: PartialEq
609
where
610
    TOwned: Borrow<Self> + PartialEq<Self>,
611
{
612
    /// Returns the value as a prefix-range, which will match all values that
613
    /// start with `self`.
614
    fn to_prefix_range(&'a self) -> RangeRef<'a, TOwned, Self>;
615
}
616

            
617
20
fn next_byte_sequence(start: &[u8]) -> Option<Vec<u8>> {
618
20
    let mut end = start.to_vec();
619
    // Modify the last byte by adding one. If it would wrap, we proceed to the
620
    // next byte.
621
30
    while let Some(last_byte) = end.pop() {
622
20
        if let Some(next) = last_byte.checked_add(1) {
623
10
            end.push(next);
624
10
            return Some(end);
625
10
        }
626
    }
627

            
628
10
    None
629
20
}
630

            
631
impl<'k> Key<'k> for Cow<'k, [u8]> {
632
    const CAN_OWN_BYTES: bool = true;
633

            
634
4
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
635
4
        Ok(bytes.into_borrowed())
636
4
    }
637
}
638

            
639
impl<'k, 'ke> KeyEncoding<Cow<'ke, [u8]>> for Cow<'k, [u8]> {
640
    type Error = Infallible;
641

            
642
    const LENGTH: Option<usize> = None;
643

            
644
    fn describe<Visitor>(visitor: &mut Visitor)
645
    where
646
        Visitor: KeyVisitor,
647
    {
648
        visitor.visit_type(KeyKind::Bytes);
649
    }
650

            
651
1
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
652
1
        Ok(self.clone())
653
1
    }
654
}
655

            
656
macro_rules! impl_u8_slice_key_encoding {
657
    ($type:ty) => {
658
        impl<'k> KeyEncoding<$type> for &'k [u8] {
659
            type Error = Infallible;
660

            
661
            const LENGTH: Option<usize> = None;
662

            
663
            fn describe<Visitor>(visitor: &mut Visitor)
664
            where
665
                Visitor: KeyVisitor,
666
            {
667
                visitor.visit_type(KeyKind::Bytes)
668
            }
669

            
670
            fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
671
                Ok(Cow::Borrowed(self))
672
            }
673
        }
674
    };
675
}
676

            
677
impl_u8_slice_key_encoding!(Cow<'k, [u8]>);
678

            
679
impl<'a, 'k> IntoPrefixRange<'a, Self> for Cow<'k, [u8]> {
680
4
    fn to_prefix_range(&'a self) -> RangeRef<'a, Self> {
681
4
        if let Some(next) = next_byte_sequence(self) {
682
2
            RangeRef {
683
2
                start: BoundRef::borrowed(Bound::Included(self)),
684
2
                end: BoundRef::owned(Bound::Excluded(Cow::Owned(next))),
685
2
            }
686
        } else {
687
2
            RangeRef {
688
2
                start: BoundRef::borrowed(Bound::Included(self)),
689
2
                end: BoundRef::Unbounded,
690
2
            }
691
        }
692
4
    }
693
}
694

            
695
impl<'a, 'k, TOwned, TBorrowed> Key<'k> for MaybeOwned<'a, TOwned, TBorrowed>
696
where
697
    TBorrowed: KeyEncoding<TOwned, Error = TOwned::Error> + PartialEq + ?Sized,
698
    TOwned: Key<'k> + PartialEq<TBorrowed>,
699
{
700
    const CAN_OWN_BYTES: bool = TOwned::CAN_OWN_BYTES;
701

            
702
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
703
        TOwned::from_ord_bytes(bytes).map(Self::Owned)
704
    }
705
}
706

            
707
impl<'a, 'k, TOwned, TBorrowed> KeyEncoding<Self> for MaybeOwned<'a, TOwned, TBorrowed>
708
where
709
    TBorrowed: KeyEncoding<TOwned, Error = TOwned::Error> + PartialEq + ?Sized,
710
    TOwned: Key<'k> + PartialEq<TBorrowed>,
711
{
712
    type Error = TOwned::Error;
713

            
714
    const LENGTH: Option<usize> = TBorrowed::LENGTH;
715

            
716
    fn describe<Visitor>(visitor: &mut Visitor)
717
    where
718
        Visitor: KeyVisitor,
719
    {
720
        TBorrowed::describe(visitor);
721
    }
722

            
723
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
724
        match self {
725
            MaybeOwned::Owned(value) => value.as_ord_bytes(),
726
            MaybeOwned::Borrowed(value) => value.as_ord_bytes(),
727
        }
728
    }
729
}
730

            
731
1
#[test]
732
1
fn cow_prefix_range_tests() {
733
1
    use std::ops::RangeBounds;
734
1
    assert!(Cow::<'_, [u8]>::Borrowed(b"a")
735
1
        .to_prefix_range()
736
1
        .contains(&Cow::Borrowed(&b"aa"[..])));
737
1
    assert!(!Cow::<'_, [u8]>::Borrowed(b"a")
738
1
        .to_prefix_range()
739
1
        .contains(&Cow::Borrowed(&b"b"[..])));
740
1
    assert!(Cow::<'_, [u8]>::Borrowed(b"\xff")
741
1
        .to_prefix_range()
742
1
        .contains(&Cow::Borrowed(&b"\xff\xff"[..])));
743
1
    assert!(!Cow::<'_, [u8]>::Borrowed(b"\xff")
744
1
        .to_prefix_range()
745
1
        .contains(&Cow::Borrowed(&b"\xfe"[..])));
746
1
}
747

            
748
impl<'k> Key<'k> for Vec<u8> {
749
    const CAN_OWN_BYTES: bool = true;
750

            
751
8
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
752
8
        Ok(bytes.into_owned())
753
8
    }
754
}
755

            
756
impl KeyEncoding<Self> for Vec<u8> {
757
    type Error = Infallible;
758

            
759
    const LENGTH: Option<usize> = None;
760

            
761
1
    fn describe<Visitor>(visitor: &mut Visitor)
762
1
    where
763
1
        Visitor: KeyVisitor,
764
1
    {
765
1
        visitor.visit_type(KeyKind::Bytes);
766
1
    }
767

            
768
11
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
769
11
        Ok(Cow::Borrowed(self))
770
11
    }
771
}
772

            
773
impl_u8_slice_key_encoding!(Vec<u8>);
774

            
775
impl<'k> IntoPrefixRange<'k, Self> for Vec<u8> {
776
4
    fn to_prefix_range(&'k self) -> RangeRef<'k, Self> {
777
4
        if let Some(next) = next_byte_sequence(self) {
778
2
            RangeRef {
779
2
                start: BoundRef::borrowed(Bound::Included(self)),
780
2
                end: BoundRef::owned(Bound::Excluded(next)),
781
2
            }
782
        } else {
783
2
            RangeRef {
784
2
                start: BoundRef::borrowed(Bound::Included(self)),
785
2
                end: BoundRef::Unbounded,
786
2
            }
787
        }
788
4
    }
789
}
790

            
791
impl<'k, const N: usize> Key<'k> for [u8; N] {
792
    const CAN_OWN_BYTES: bool = false;
793

            
794
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
795
        if bytes.as_ref().len() == N {
796
            let mut array = [0; N];
797
            array.copy_from_slice(bytes.as_ref());
798
            Ok(array)
799
        } else {
800
            Err(IncorrectByteLength)
801
        }
802
    }
803
}
804

            
805
impl<const N: usize> KeyEncoding<Self> for [u8; N] {
806
    type Error = IncorrectByteLength;
807

            
808
    const LENGTH: Option<usize> = Some(N);
809

            
810
    fn describe<Visitor>(visitor: &mut Visitor)
811
    where
812
        Visitor: KeyVisitor,
813
    {
814
        visitor.visit_type(KeyKind::Bytes);
815
    }
816

            
817
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
818
        Ok(Cow::Borrowed(self))
819
    }
820
}
821

            
822
1
#[test]
823
1
fn vec_prefix_range_tests() {
824
1
    use std::ops::RangeBounds;
825
1
    assert!(b"a".to_vec().to_prefix_range().contains(&b"aa".to_vec()));
826
1
    assert!(!b"a".to_vec().to_prefix_range().contains(&b"b".to_vec()));
827
1
    assert!(b"\xff"
828
1
        .to_vec()
829
1
        .to_prefix_range()
830
1
        .contains(&b"\xff\xff".to_vec()));
831
1
    assert!(!b"\xff"
832
1
        .to_vec()
833
1
        .to_prefix_range()
834
1
        .contains(&b"\xfe".to_vec()));
835
1
}
836

            
837
impl<'k> Key<'k> for ArcBytes<'k> {
838
    const CAN_OWN_BYTES: bool = true;
839

            
840
    fn from_ord_bytes<'b>(bytes: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
841
        Ok(Self::from(bytes.into_borrowed()))
842
    }
843
}
844

            
845
impl<'k> KeyEncoding<Self> for ArcBytes<'k> {
846
    type Error = Infallible;
847

            
848
    const LENGTH: Option<usize> = None;
849

            
850
    fn describe<Visitor>(visitor: &mut Visitor)
851
    where
852
        Visitor: KeyVisitor,
853
    {
854
        visitor.visit_type(KeyKind::Bytes);
855
    }
856

            
857
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
858
        Ok(Cow::Borrowed(self))
859
    }
860
}
861

            
862
impl_u8_slice_key_encoding!(ArcBytes<'k>);
863

            
864
impl<'a, 'k> IntoPrefixRange<'a, Self> for ArcBytes<'k> {
865
4
    fn to_prefix_range(&'a self) -> RangeRef<'a, Self> {
866
4
        if let Some(next) = next_byte_sequence(self) {
867
2
            RangeRef {
868
2
                start: BoundRef::borrowed(Bound::Included(self)),
869
2
                end: BoundRef::owned(Bound::Excluded(Self::owned(next))),
870
2
            }
871
        } else {
872
2
            RangeRef {
873
2
                start: BoundRef::borrowed(Bound::Included(self)),
874
2
                end: BoundRef::Unbounded,
875
2
            }
876
        }
877
4
    }
878
}
879

            
880
1
#[test]
881
1
fn arcbytes_prefix_range_tests() {
882
1
    use std::ops::RangeBounds;
883
1
    assert!(ArcBytes::from(b"a")
884
1
        .to_prefix_range()
885
1
        .contains(&ArcBytes::from(b"aa")));
886
1
    assert!(!ArcBytes::from(b"a")
887
1
        .to_prefix_range()
888
1
        .contains(&ArcBytes::from(b"b")));
889
1
    assert!(ArcBytes::from(b"\xff")
890
1
        .to_prefix_range()
891
1
        .contains(&ArcBytes::from(b"\xff\xff")));
892
1
    assert!(!ArcBytes::from(b"\xff")
893
1
        .to_prefix_range()
894
1
        .contains(&ArcBytes::from(b"\xfe")));
895
1
}
896

            
897
impl<'k> Key<'k> for CowBytes<'k> {
898
    const CAN_OWN_BYTES: bool = true;
899

            
900
    fn from_ord_bytes<'b>(bytes: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
901
        Ok(Self(bytes.into_borrowed()))
902
    }
903
}
904

            
905
impl<'k> KeyEncoding<Self> for CowBytes<'k> {
906
    type Error = Infallible;
907

            
908
    const LENGTH: Option<usize> = None;
909

            
910
    fn describe<Visitor>(visitor: &mut Visitor)
911
    where
912
        Visitor: KeyVisitor,
913
    {
914
        visitor.visit_type(KeyKind::Bytes);
915
    }
916

            
917
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
918
        Ok(self.0.clone())
919
    }
920
}
921

            
922
impl_u8_slice_key_encoding!(CowBytes<'k>);
923

            
924
impl<'a, 'k> IntoPrefixRange<'a, Self> for CowBytes<'k> {
925
4
    fn to_prefix_range(&'a self) -> RangeRef<'_, Self> {
926
4
        if let Some(next) = next_byte_sequence(self) {
927
2
            RangeRef {
928
2
                start: BoundRef::borrowed(Bound::Included(self)),
929
2
                end: BoundRef::owned(Bound::Excluded(Self::from(next))),
930
2
            }
931
        } else {
932
2
            RangeRef {
933
2
                start: BoundRef::borrowed(Bound::Included(self)),
934
2
                end: BoundRef::Unbounded,
935
2
            }
936
        }
937
4
    }
938
}
939

            
940
1
#[test]
941
1
fn cowbytes_prefix_range_tests() {
942
1
    use std::ops::RangeBounds;
943
1
    assert!(CowBytes::from(&b"a"[..])
944
1
        .to_prefix_range()
945
1
        .contains(&CowBytes::from(&b"aa"[..])));
946
1
    assert!(!CowBytes::from(&b"a"[..])
947
1
        .to_prefix_range()
948
1
        .contains(&CowBytes::from(&b"b"[..])));
949
1
    assert!(CowBytes::from(&b"\xff"[..])
950
1
        .to_prefix_range()
951
1
        .contains(&CowBytes::from(&b"\xff\xff"[..])));
952
1
    assert!(!CowBytes::from(&b"\xff"[..])
953
1
        .to_prefix_range()
954
1
        .contains(&CowBytes::from(&b"\xfe"[..])));
955
1
}
956

            
957
impl<'k> Key<'k> for Bytes {
958
    const CAN_OWN_BYTES: bool = true;
959

            
960
    fn from_ord_bytes<'b>(bytes: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
961
        Ok(Self(bytes.into_owned()))
962
    }
963
}
964

            
965
impl KeyEncoding<Self> for Bytes {
966
    type Error = Infallible;
967

            
968
    const LENGTH: Option<usize> = None;
969

            
970
1
    fn describe<Visitor>(visitor: &mut Visitor)
971
1
    where
972
1
        Visitor: KeyVisitor,
973
1
    {
974
1
        visitor.visit_type(KeyKind::Bytes);
975
1
    }
976

            
977
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
978
        Ok(Cow::Borrowed(self))
979
    }
980
}
981

            
982
impl_u8_slice_key_encoding!(Bytes);
983

            
984
impl<'a> IntoPrefixRange<'a, Self> for Bytes {
985
4
    fn to_prefix_range(&'a self) -> RangeRef<'a, Self> {
986
4
        if let Some(next) = next_byte_sequence(self) {
987
2
            RangeRef {
988
2
                start: BoundRef::borrowed(Bound::Included(self)),
989
2
                end: BoundRef::owned(Bound::Excluded(Self::from(next))),
990
2
            }
991
        } else {
992
2
            RangeRef {
993
2
                start: BoundRef::borrowed(Bound::Included(self)),
994
2
                end: BoundRef::Unbounded,
995
2
            }
996
        }
997
4
    }
998
}
999

            
1
#[test]
1
fn bytes_prefix_range_tests() {
1
    use std::ops::RangeBounds;
1
    assert!(Bytes::from(b"a".to_vec())
1
        .to_prefix_range()
1
        .contains(&Bytes::from(b"aa".to_vec())));
1
    assert!(!Bytes::from(b"a".to_vec())
1
        .to_prefix_range()
1
        .contains(&Bytes::from(b"b".to_vec())));
1
    assert!(Bytes::from(b"\xff".to_vec())
1
        .to_prefix_range()
1
        .contains(&Bytes::from(b"\xff\xff".to_vec())));
1
    assert!(!Bytes::from(b"\xff".to_vec())
1
        .to_prefix_range()
1
        .contains(&Bytes::from(b"\xfe".to_vec())));
1
}

            
impl<'k> Key<'k> for String {
    const CAN_OWN_BYTES: bool = true;

            
552810
    fn from_ord_bytes<'b>(bytes: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
552810
        Self::from_utf8(bytes.into_owned())
552810
    }
}

            
impl KeyEncoding<Self> for String {
    type Error = FromUtf8Error;

            
    const LENGTH: Option<usize> = None;

            
3361
    fn describe<Visitor>(visitor: &mut Visitor)
3361
    where
3361
        Visitor: KeyVisitor,
3361
    {
3361
        visitor.visit_type(KeyKind::String);
3361
    }

            
150327
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
150327
        Ok(Cow::Borrowed(self.as_bytes()))
150327
    }
}

            
impl KeyEncoding<String> for str {
    type Error = FromUtf8Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::String);
    }

            
399844
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
399844
        Ok(Cow::Borrowed(self.as_bytes()))
399844
    }
}

            
impl<'a> IntoPrefixRange<'a, Self> for String {
6
    fn to_prefix_range(&'a self) -> RangeRef<'a, Self> {
6
        let mut bytes = self.as_bytes().to_vec();
6
        for (index, char) in self.char_indices().rev() {
6
            let mut next_char = u32::from(char) + 1;
6
            if next_char == 0xd800 {
                next_char = 0xE000;
6
            } else if next_char > u32::from(char::MAX) {
2
                continue;
4
            }

            
4
            let mut char_bytes = [0; 6];
4
            bytes.splice(
4
                index..,
4
                char::try_from(next_char)
4
                    .unwrap()
4
                    .encode_utf8(&mut char_bytes)
4
                    .bytes(),
4
            );
4
            return RangeRef {
4
                start: BoundRef::borrowed(Bound::Included(self)),
4
                end: BoundRef::owned(Bound::Excluded(Self::from_utf8(bytes).unwrap())),
4
            };
        }

            
2
        RangeRef {
2
            start: BoundRef::borrowed(Bound::Included(self)),
2
            end: BoundRef::Unbounded,
2
        }
6
    }
}

            
impl<'a> IntoPrefixRange<'a, String> for str {
40
    fn to_prefix_range(&'a self) -> RangeRef<'a, String, Self> {
40
        let mut bytes = self.as_bytes().to_vec();
40
        for (index, char) in self.char_indices().rev() {
40
            let mut next_char = u32::from(char) + 1;
40
            if next_char == 0xd800 {
                next_char = 0xE000;
40
            } else if next_char > u32::from(char::MAX) {
                continue;
40
            }

            
40
            let mut char_bytes = [0; 6];
40
            bytes.splice(
40
                index..,
40
                char::try_from(next_char)
40
                    .unwrap()
40
                    .encode_utf8(&mut char_bytes)
40
                    .bytes(),
40
            );
40
            return RangeRef {
40
                start: BoundRef::borrowed(Bound::Included(self)),
40
                end: BoundRef::owned(Bound::Excluded(String::from_utf8(bytes).unwrap())),
40
            };
        }

            
        RangeRef {
            start: BoundRef::borrowed(Bound::Included(self)),
            end: BoundRef::Unbounded,
        }
40
    }
}

            
impl<'k> Key<'k> for Cow<'k, str> {
    const CAN_OWN_BYTES: bool = true;

            
120
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
120
        match bytes.into_borrowed() {
120
            Cow::Borrowed(bytes) => std::str::from_utf8(bytes).map(Cow::Borrowed),
            Cow::Owned(bytes) => String::from_utf8(bytes)
                .map(Cow::Owned)
                .map_err(|e| e.utf8_error()),
        }
120
    }
}

            
impl<'k> KeyEncoding<Self> for Cow<'k, str> {
    type Error = std::str::Utf8Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::String);
    }

            
4440
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
4440
        Ok(Cow::Borrowed(self.as_bytes()))
4440
    }
}

            
1
#[test]
1
fn string_prefix_range_tests() {
1
    use std::ops::RangeBounds;
1
    assert!(String::from("a")
1
        .to_prefix_range()
1
        .contains(&String::from("aa")));
1
    assert!(!String::from("a")
1
        .to_prefix_range()
1
        .contains(&String::from("b")));
1
    assert!(String::from("\u{d799}")
1
        .to_prefix_range()
1
        .contains(&String::from("\u{d799}a")));
1
    assert!(!String::from("\u{d799}")
1
        .to_prefix_range()
1
        .contains(&String::from("\u{e000}")));
1
    assert!(String::from("\u{10ffff}")
1
        .to_prefix_range()
1
        .contains(&String::from("\u{10ffff}a")));
1
    assert!(!String::from("\u{10ffff}")
1
        .to_prefix_range()
1
        .contains(&String::from("\u{10fffe}")));
1
}

            
impl<'k> Key<'k> for () {
    const CAN_OWN_BYTES: bool = false;

            
121
    fn from_ord_bytes<'b>(_: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
121
        Ok(())
121
    }
}

            
impl KeyEncoding<Self> for () {
    type Error = Infallible;

            
    const LENGTH: Option<usize> = Some(0);

            
640
    fn describe<Visitor>(visitor: &mut Visitor)
640
    where
640
        Visitor: KeyVisitor,
640
    {
640
        visitor.visit_type(KeyKind::Unit);
640
    }

            
81
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
81
        Ok(Cow::default())
81
    }
}

            
impl<'k> Key<'k> for bool {
    const CAN_OWN_BYTES: bool = false;

            
2
    fn from_ord_bytes<'b>(bytes: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
2
        let bytes = bytes.as_ref();
2
        if bytes.is_empty() || bytes[0] == 0 {
1
            Ok(false)
        } else {
1
            Ok(true)
        }
2
    }
}

            
impl KeyEncoding<Self> for bool {
    type Error = Infallible;

            
    const LENGTH: Option<usize> = Some(1);

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Bool);
    }

            
2
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
2
        if *self {
1
            Ok(Cow::Borrowed(&[1_u8]))
        } else {
1
            Ok(Cow::Borrowed(&[0_u8]))
        }
2
    }
}

            
macro_rules! count_args {
    () => (0usize);
    ( $arg:tt $($remaining:tt)* ) => (1usize + count_args!($($remaining)*));
}

            
macro_rules! impl_key_for_tuple {
    ($(($index:tt, $varname:ident, $generic:ident)),+) => {
        impl<'k, $($generic),+> Key<'k> for ($($generic),+,)
        where
            $($generic: for<'ke> Key<'ke>),+
        {
            const CAN_OWN_BYTES: bool = false;
87391
            fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
87391
                let mut decoder = CompositeKeyDecoder::default_for(bytes);
87391
                $(let $varname = decoder.decode::<$generic>()?;)+
87391
                decoder.finish()?;

            
87391
                Ok(($($varname),+,))
87391
            }
        }

            
        impl<$($generic),+> KeyEncoding<Self> for ($($generic),+,)
        where
            $($generic: for<'k> Key<'k>),+
        {
            type Error = CompositeKeyError;

            
            const LENGTH: Option<usize> = match ($($generic::LENGTH),+,) {
                ($(Some($varname)),+,) => Some($($varname +)+ 0),
                _ => None,
            };

            
1
            fn describe<Visitor>(visitor: &mut Visitor)
1
            where
1
                Visitor: KeyVisitor,
1
            {
1
                visitor.visit_composite(CompositeKind::Tuple, count_args!($($generic) +));
1
                $($generic::describe(visitor);)+
1
            }

            
87391
            fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
87391
                let mut encoder = CompositeKeyEncoder::default();
87391

            
87391
                $(encoder.encode(&self.$index)?;)+

            
87391
                Ok(Cow::Owned(encoder.finish()))
87391
            }
        }
    };
}

            
impl_key_for_tuple!((0, t1, T1));
impl_key_for_tuple!((0, t1, T1), (1, t2, T2));
impl_key_for_tuple!((0, t1, T1), (1, t2, T2), (2, t3, T3));
impl_key_for_tuple!((0, t1, T1), (1, t2, T2), (2, t3, T3), (3, t4, T4));
impl_key_for_tuple!(
    (0, t1, T1),
    (1, t2, T2),
    (2, t3, T3),
    (3, t4, T4),
    (4, t5, T5)
);
impl_key_for_tuple!(
    (0, t1, T1),
    (1, t2, T2),
    (2, t3, T3),
    (3, t4, T4),
    (4, t5, T5),
    (5, t6, T6)
);
impl_key_for_tuple!(
    (0, t1, T1),
    (1, t2, T2),
    (2, t3, T3),
    (3, t4, T4),
    (4, t5, T5),
    (5, t6, T6),
    (6, t7, T7)
);
impl_key_for_tuple!(
    (0, t1, T1),
    (1, t2, T2),
    (2, t3, T3),
    (3, t4, T4),
    (4, t5, T5),
    (5, t6, T6),
    (6, t7, T7),
    (7, t8, T8)
);

            
/// Encodes multiple [`KeyEncoding`] implementors into a single byte buffer,
/// preserving the ordering guarantees necessary for [`Key`].
///
/// The produced bytes can be decoded using [`CompositeKeyDecoder`].
pub struct CompositeKeyEncoder<NullHandling = EscapeNullBytes> {
    bytes: Vec<u8>,
    encoded_lengths: Vec<u16>,
    null_handling: NullHandling,
}

            
impl<NullHandling> CompositeKeyEncoder<NullHandling>
where
    NullHandling: CompositeKeyNullHandler,
{
    /// Encodes a single [`KeyEncoding`] implementing value.
    ///
    /// ```rust
    /// # use bonsaidb_core::key::{ByteSource, CompositeKeyEncoder, CompositeKeyDecoder};
    ///
    /// let value1 = String::from("hello");
    /// let value2 = 42_u32;
    /// let mut encoder = CompositeKeyEncoder::default();
    /// encoder.encode(&value1).unwrap();
    /// encoder.encode(&value2).unwrap();
    /// let encoded = encoder.finish();
    ///
    /// let mut decoder = CompositeKeyDecoder::default_for(ByteSource::Borrowed(&encoded));
    /// let decoded_string = decoder.decode::<String>().unwrap();
    /// assert_eq!(decoded_string, value1);
    /// let decoded_u32 = decoder.decode::<u32>().unwrap();
    /// assert_eq!(decoded_u32, value2);
    /// decoder.finish().expect("trailing bytes");
    /// ```
670217
    pub fn encode<'k, K: Key<'k>, T: KeyEncoding<K> + ?Sized>(
670217
        &mut self,
670217
        value: &'k T,
670217
    ) -> Result<(), CompositeKeyError> {
670217
        let mut encoded = T::as_ord_bytes(value).map_err(CompositeKeyError::new)?;
670217
        if T::LENGTH.is_none() {
277
            self.null_handling.handle_nulls(&mut encoded)?;
276
            let encoded_length = u16::try_from(encoded.len())?;
276
            self.encoded_lengths.push(encoded_length);
669940
        }
670216
        self.bytes.extend_from_slice(&encoded);
670216
        if T::LENGTH.is_none() {
276
            // With variable length data, we need the key to have a delimiter to
276
            // ensure that "a" sorts before "aa".
276
            self.bytes.push(0);
670190
        }
670216
        Ok(())
670217
    }

            
    /// Finishes encoding the field and returns the encoded bytes.
    #[must_use]
    #[allow(clippy::missing_panics_doc)] // All unreachable
87580
    pub fn finish(mut self) -> Vec<u8> {
87580
        self.bytes.reserve_exact(self.encoded_lengths.len() * 2);
87682
        for length in self.encoded_lengths.into_iter().rev() {
306
            match length {
306
                0..=0x7F => {
300
                    self.bytes.push(u8::try_from(length).unwrap());
300
                }
6
                0x80..=0x3FFF => {
4
                    self.bytes.push(u8::try_from(length >> 7).unwrap());
4
                    self.bytes
4
                        .push(u8::try_from((length & 0x7F) | 0x80).unwrap());
4
                }
2
                0x4000.. => {
2
                    self.bytes.push(u8::try_from(length >> 14).unwrap());
2
                    self.bytes
2
                        .push(u8::try_from(((length >> 7) & 0x7F) | 0x80).unwrap());
2
                    self.bytes
2
                        .push(u8::try_from((length & 0x7F) | 0x80).unwrap());
2
                }
            }
        }
87580
        self.bytes
87580
    }
}

            
impl Default for CompositeKeyEncoder<EscapeNullBytes> {
91272
    fn default() -> Self {
91272
        Self {
91272
            bytes: Vec::new(),
91272
            encoded_lengths: Vec::new(),
91272
            null_handling: EscapeNullBytes,
91272
        }
91272
    }
}

            
impl CompositeKeyEncoder<AllowNullBytes> {
    /// Returns an encoder that allows null bytes in variable length fields.
    ///
    /// See [`CompositeKeyFieldContainsNullByte`] for information about the edge
    /// cases this may introduce.
    #[must_use]
1
    pub const fn allowing_null_bytes() -> Self {
1
        Self {
1
            bytes: Vec::new(),
1
            encoded_lengths: Vec::new(),
1
            null_handling: AllowNullBytes,
1
        }
1
    }
}

            
impl CompositeKeyEncoder<DenyNullBytes> {
    /// Returns an encoder that denies null bytes in variable length fields by
    /// returning an error when any null bytes are detected.
    #[must_use]
1
    pub const fn denying_null_bytes() -> Self {
1
        Self {
1
            bytes: Vec::new(),
1
            encoded_lengths: Vec::new(),
1
            null_handling: DenyNullBytes,
1
        }
1
    }
}

            
/// Escapes null bytes in variable length fields in composite keys. This option
/// ensures proper sort order is maintained even when null bytes are used within
/// vairable fields.
///
/// To see more information about the edge case encoding prevents, see
/// [`CompositeKeyFieldContainsNullByte`].
#[derive(Default, Debug, Clone, Copy)]
pub struct EscapeNullBytes;
/// Prevents checking for null bytes in variable length fields in composite
/// keys. [`Key`] types that have a fixed width have no edge cases with null
/// bytes. See [`CompositeKeyFieldContainsNullByte`] for an explanation and
/// example of the edge case introduced when allowing null bytes to be used
/// without escaping.
#[derive(Default, Debug, Clone, Copy)]
pub struct AllowNullBytes;
#[derive(Default, Debug, Clone, Copy)]
/// Checks for null bytes when encoding variable length fields in composite keys
/// and returns an error if any are encountered. This prevents extra processing
/// when encoding fields, but may introduce incorrect sort ordering (see
/// [`CompositeKeyFieldContainsNullByte`] for more).
pub struct DenyNullBytes;

            
/// A null-byte handling approach for [`CompositeKeyEncoder`] and
/// [`CompositeKeyDecoder`]. This type is only used when the fields being
/// encoded are variable length.
///
/// Ensuring proper sort order with composite keys requires special handling of
/// null bytes. The safest option is to use [`EscapeNullBytes`], but this option
/// will cause extra allocations when keys contain null bytes.
/// [`AllowNullBytes`] allows null-bytes through without any extra checks, but
/// their usage can cause incorrect sorting behavior. See
/// [`CompositeKeyFieldContainsNullByte`] for more information on this edge
/// case. The last implementation is [`DenyNullBytes`] which checks for null
/// bytes when encoding and returns an error if any are encountered. When
/// decoding with this option, there is no extra processing performed before
/// decoding individual fields.
pub trait CompositeKeyNullHandler {
    /// Process the null bytes in `field_bytes`, if needed.
    fn handle_nulls(&self, field_bytes: &mut Cow<'_, [u8]>) -> Result<(), CompositeKeyError>;
    /// Decode the null bytes in `encoded`, if needed.
    fn decode_nulls_if_needed<'b, 'e>(
        &self,
        encoded: ByteSource<'b, 'e>,
    ) -> Result<ByteSource<'b, 'e>, CompositeKeyError>;
}

            
impl CompositeKeyNullHandler for DenyNullBytes {
    #[inline]
1
    fn handle_nulls(&self, encoded: &mut Cow<'_, [u8]>) -> Result<(), CompositeKeyError> {
1
        if encoded.iter().any(|b| *b == 0) {
1
            Err(CompositeKeyError::new(io::Error::new(
1
                ErrorKind::InvalidData,
1
                CompositeKeyFieldContainsNullByte,
1
            )))
        } else {
            Ok(())
        }
1
    }

            
    #[inline]
1
    fn decode_nulls_if_needed<'b, 'e>(
1
        &self,
1
        encoded: ByteSource<'b, 'e>,
1
    ) -> Result<ByteSource<'b, 'e>, CompositeKeyError> {
1
        Ok(encoded)
1
    }
}

            
impl CompositeKeyNullHandler for AllowNullBytes {
    #[inline]
1
    fn handle_nulls(&self, _encoded: &mut Cow<'_, [u8]>) -> Result<(), CompositeKeyError> {
1
        Ok(())
1
    }

            
    #[inline]
1
    fn decode_nulls_if_needed<'b, 'e>(
1
        &self,
1
        encoded: ByteSource<'b, 'e>,
1
    ) -> Result<ByteSource<'b, 'e>, CompositeKeyError> {
1
        Ok(encoded)
1
    }
}

            
impl CompositeKeyNullHandler for EscapeNullBytes {
    #[inline]
275
    fn handle_nulls(&self, unescaped: &mut Cow<'_, [u8]>) -> Result<(), CompositeKeyError> {
275
        let null_bytes = bytecount::count(unescaped, 0);
275
        if null_bytes > 0 {
1
            let mut null_encoded = Vec::with_capacity(unescaped.len() + null_bytes);
3
            for unescaped in unescaped.as_ref() {
3
                if *unescaped == 0 {
1
                    null_encoded.extend_from_slice(&[0, 0]);
2
                } else {
2
                    null_encoded.push(*unescaped);
2
                }
            }
1
            *unescaped = Cow::Owned(null_encoded);
274
        }
275
        Ok(())
275
    }

            
    #[inline]
218
    fn decode_nulls_if_needed<'b, 'e>(
218
        &self,
218
        mut encoded: ByteSource<'b, 'e>,
218
    ) -> Result<ByteSource<'b, 'e>, CompositeKeyError> {
        // Find the first null byte
218
        if let Some(mut index) = encoded
218
            .iter()
218
            .enumerate()
67742
            .find_map(|(index, b)| (*b == 0).then_some(index))
        {
            // The field has at least one null byte, we need to mutate the
            // bytes.
1
            let mut bytes = encoded.into_owned();
            loop {
                // Check that the next byte is a null. If so, we remove the
                // current byte and look for the next null.
1
                let next_index = index + 1;
1
                if Some(0) == bytes.get(next_index).copied() {
1
                    bytes.remove(index);
1
                    index = next_index;
1
                } else {
                    todo!("error: unescaped null byte")
                    // return Err(CompositeKeyError::new("unescaped null byte encountered"));
                }

            
                // Find the next null byte, if one exists.
1
                let Some(next_index) = bytes[index..]
1
                    .iter()
1
                    .enumerate()
1
                    .find_map(|(index, b)| (*b == 0).then_some(index))
                else {
1
                    break;
                };
                index += next_index;
            }
1
            encoded = ByteSource::Owned(bytes);
217
        }
218
        Ok(encoded)
218
    }
}

            
/// Null bytes in variable fields encoded with [`CompositeKeyEncoder`] can cause
/// sort order to misbehave.
///
/// Consider these tuples:
///
/// | Tuple          | Encoded          |
/// |----------------|------------------|
/// | `("a", "b")`   | `9700 9800 0101` |
/// | `("a\0", "a")` | `9700 9700 0101` |
///
/// The encoded bytes, when compared, will produce a different sort result than
/// when comparing the tuples.
///
/// By default, [`CompositeKeyEncoder`] checks for null bytes and returns an
/// error when a null byte is found. See
/// [`CompositeKeyEncoder::allowing_null_bytes()`] if you wish
/// to allow null bytes despite this edge case.
#[derive(thiserror::Error, Debug)]
#[error("a variable length field contained a null byte.")]
pub struct CompositeKeyFieldContainsNullByte;

            
/// Decodes multiple [`Key`] values from a byte slice previously encoded with
/// [`CompositeKeyEncoder`].
pub struct CompositeKeyDecoder<'key, 'ephemeral, NullHandling = EscapeNullBytes> {
    bytes: ByteSource<'key, 'ephemeral>,
    offset: usize,
    end: usize,
    null_handling: NullHandling,
}

            
impl<'key, 'ephemeral> CompositeKeyDecoder<'key, 'ephemeral, EscapeNullBytes> {
    /// Returns a decoder for `bytes` that decodes escaped null bytes.
    ///
    /// This function is compatible with keys encoded with
    /// [`CompositeKeyEncoder::default()`].
    #[must_use]
90352
    pub fn default_for(bytes: ByteSource<'key, 'ephemeral>) -> Self {
90352
        Self {
90352
            end: bytes.as_ref().len(),
90352
            bytes,
90352
            offset: 0,
90352
            null_handling: EscapeNullBytes,
90352
        }
90352
    }
}

            
impl<'key, 'ephemeral> CompositeKeyDecoder<'key, 'ephemeral, AllowNullBytes> {
    /// Returns a decoder for `bytes` that ignores null bytes.
    ///
    /// This function is compatible with keys encoded with
    /// [`CompositeKeyEncoder::allowing_null_bytes()`].
    #[must_use]
1
    pub fn allowing_null_bytes(bytes: ByteSource<'key, 'ephemeral>) -> Self {
1
        Self {
1
            end: bytes.as_ref().len(),
1
            bytes,
1
            offset: 0,
1
            null_handling: AllowNullBytes,
1
        }
1
    }
}

            
impl<'key, 'ephemeral> CompositeKeyDecoder<'key, 'ephemeral, DenyNullBytes> {
    /// Returns a decoder for `bytes` that ignores null bytes.
    ///
    /// This function is compatible with keys encoded with
    /// [`CompositeKeyEncoder::denying_null_bytes()`].
    #[must_use]
1
    pub fn denying_null_bytes(bytes: ByteSource<'key, 'ephemeral>) -> Self {
1
        Self {
1
            end: bytes.as_ref().len(),
1
            bytes,
1
            offset: 0,
1
            null_handling: DenyNullBytes,
1
        }
1
    }
}

            
impl<'key, 'ephemeral, NullHandling> CompositeKeyDecoder<'key, 'ephemeral, NullHandling>
where
    NullHandling: CompositeKeyNullHandler,
{
    /// Decodes a value previously encoded using [`CompositeKeyEncoder`]. Calls
    /// to decode must be made in the same order as the values were encoded in.
    ///
    /// ```rust
    /// # use bonsaidb_core::key::{ByteSource, CompositeKeyEncoder, CompositeKeyDecoder};
    ///
    /// let value1 = String::from("hello");
    /// let value2 = 42_u32;
    /// let mut encoder = CompositeKeyEncoder::default();
    /// encoder.encode(&value1).unwrap();
    /// encoder.encode(&value2).unwrap();
    /// let encoded = encoder.finish();
    ///
    /// let mut decoder = CompositeKeyDecoder::default_for(ByteSource::Borrowed(&encoded));
    /// let decoded_string = decoder.decode::<String>().unwrap();
    /// assert_eq!(decoded_string, value1);
    /// let decoded_u32 = decoder.decode::<u32>().unwrap();
    /// assert_eq!(decoded_u32, value2);
    /// decoder.finish().expect("trailing bytes");
    /// ```
670157
    pub fn decode<T: Key<'key>>(&mut self) -> Result<T, CompositeKeyError> {
670157
        let length = if let Some(length) = T::LENGTH {
669937
            length
        } else {
            // Read a variable-encoded length from the tail of the bytes.
220
            let mut length = 0;
220
            let mut found_end = false;
220

            
220
            let bytes = self.bytes.as_ref();
220

            
220
            let iterator = bytes[self.offset..self.end]
220
                .iter()
220
                .copied()
220
                .rev()
220
                .enumerate();

            
228
            for (index, byte) in iterator {
228
                length |= usize::from(byte & 0x7f) << (index * 7);
228

            
228
                if byte & 0x80 == 0 {
220
                    self.end = self.end - index - 1;
220
                    found_end = true;
220
                    break;
8
                }
            }

            
220
            if !found_end {
                return Err(CompositeKeyError::new(io::Error::from(
                    ErrorKind::UnexpectedEof,
                )));
220
            }
220
            length
        };
670157
        let end = self.offset + length;
670157
        if end <= self.end {
670157
            let mut bytes = match &self.bytes {
670157
                ByteSource::Borrowed(bytes) => ByteSource::Borrowed(&bytes[self.offset..end]),
                ByteSource::Ephemeral(bytes) => ByteSource::Ephemeral(&bytes[self.offset..end]),
                ByteSource::Owned(bytes) => ByteSource::Ephemeral(&bytes[self.offset..end]),
            };
670157
            if T::LENGTH.is_none() {
220
                bytes = self.null_handling.decode_nulls_if_needed(bytes)?;
669937
            }
670157
            let decoded = T::from_ord_bytes(bytes).map_err(CompositeKeyError::new)?;
670157
            self.offset = end;
670157
            if T::LENGTH.is_none() {
220
                // Variable fields always have an extra null byte to delimit the data
220
                self.offset += 1;
670133
            }
670157
            Ok(decoded)
        } else {
            Err(CompositeKeyError::new(io::Error::from(
                ErrorKind::UnexpectedEof,
            )))
        }
670157
    }

            
    /// Verifies the underlying byte slice has been fully consumed.
87513
    pub fn finish(self) -> Result<(), CompositeKeyError> {
87513
        if self.offset == self.end {
87513
            Ok(())
        } else {
            Err(CompositeKeyError::new(io::Error::from(
                ErrorKind::InvalidData,
            )))
        }
87513
    }
}

            
1
#[test]
1
fn composite_key_null_check_test() {
1
    let mut encoder = CompositeKeyEncoder::denying_null_bytes();
1
    encoder.encode(&vec![0_u8]).unwrap_err();
1

            
1
    let mut encoder = CompositeKeyEncoder::allowing_null_bytes();
1
    encoder.encode(&vec![0_u8]).unwrap();
1
    let bytes = encoder.finish();
1
    let mut decoder = CompositeKeyDecoder::allowing_null_bytes(ByteSource::Borrowed(&bytes));
1
    let decoded_value = decoder.decode::<Cow<'_, [u8]>>().unwrap();
1
    assert_eq!(decoded_value.as_ref(), &[0]);
1
    let mut decoder = CompositeKeyDecoder::denying_null_bytes(ByteSource::Borrowed(&bytes));
1
    let decoded_value = decoder.decode::<Cow<'_, [u8]>>().unwrap();
1
    assert_eq!(decoded_value.as_ref(), &[0]);

            
1
    let mut encoder = CompositeKeyEncoder::default();
1
    encoder.encode(&vec![1_u8, 0, 1]).unwrap();
1
    let bytes = encoder.finish();
1
    // One byte for the length, 3 original bytes, 1 escaped null, 1 field-delimiting null
1
    assert_eq!(bytes.len(), 6);
1
    let mut decoder = CompositeKeyDecoder::default_for(ByteSource::Borrowed(&bytes));
1
    let decoded_value = decoder.decode::<Cow<'_, [u8]>>().unwrap();
1
    assert_eq!(decoded_value.as_ref(), &[1, 0, 1]);
1
}

            
1
#[test]
#[allow(clippy::cognitive_complexity)] // There's no way to please clippy with this
1
fn composite_key_tests() {
1
    /// This test generates various combinations of every tuple type supported.
1
    /// Each test calls this function, which builds a second Vec containing all
1
    /// of the encoded key values. Next, both the original vec and the encoded
1
    /// vec are sorted. The encoded entries are decoded, and the sorted original
1
    /// vec is compared against the decoded vec to ensure the ordering is the
1
    /// same.
1
    ///
1
    /// The macros just make it less verbose to create the nested loops which
1
    /// create the tuples.
12
    fn verify_key_ordering<T: for<'k> Key<'k> + Ord + Eq + std::fmt::Debug>(
12
        mut cases: Vec<T>,
12
        already_ordered: bool,
12
    ) {
12
        let mut encoded = {
12
            cases
12
                .iter()
87391
                .map(|tuple| tuple.as_ord_bytes().unwrap().to_vec())
12
                .collect::<Vec<Vec<u8>>>()
12
        };
12
        if !already_ordered {
8
            cases.sort();
8
        }
12
        encoded.sort();
12
        println!("Tested {} entries", cases.len());
12
        let decoded = encoded
12
            .iter()
87391
            .map(|encoded| T::from_ord_bytes(ByteSource::Borrowed(encoded)).unwrap())
12
            .collect::<Vec<_>>();
12
        assert_eq!(cases, decoded);
12
    }
1

            
1
    // Simple mixed-length tests
1
    let a2 = (String::from("a"), 2_u8);
1
    let aa1 = (String::from("aa"), 1_u8);
1
    let aa2 = (String::from("aa"), 2_u8);
1
    let b1 = (String::from("b"), 1_u8);
1
    let b2 = (String::from("b"), 2_u8);
1
    verify_key_ordering(vec![a2.clone(), aa1.clone()], true);
1
    verify_key_ordering(vec![a2, aa1, aa2, b1, b2], true);
1

            
1
    // Two byte length (0x80)
1
    verify_key_ordering(
1
        vec![(vec![1; 127], vec![2; 128]), (vec![1; 128], vec![2; 127])],
1
        true,
1
    );
1
    // Three byte length (0x80)
1
    verify_key_ordering(
1
        vec![
1
            (vec![1; 16383], vec![1; 16384]),
1
            (vec![1; 16384], vec![2; 16383]),
1
        ],
1
        true,
1
    );
1

            
1
    let values = [0_u16, 0xFF00, 0x0FF0, 0xFF];
    macro_rules! test_enum_variations {
        ($($ident:ident),+) => {
            let mut cases = Vec::new();
            test_enum_variations!(for cases $($ident),+; $($ident),+);
            verify_key_ordering(cases, false);
        };
        (for $cases:ident $first:ident, $($ident:ident),+; $($variable:ident),+) => {
            for $first in values {
                test_enum_variations!(for $cases $($ident),+; $($variable),+);
            }
        };
        (for $cases:ident $first:ident; $($ident:ident),+) => {
            for $first in values {
                $cases.push(($($ident),+,));
            }
        };
    }
    macro_rules! recursive_test_enum_variations {
        ($ident:ident, $($last_ident:ident),+) => {
            recursive_test_enum_variations!($($last_ident),+);
            test_enum_variations!($ident, $($last_ident),+);
        };
        ($ident:ident) => {
            test_enum_variations!($ident);
        }
    }
87380
    recursive_test_enum_variations!(t1, t2, t3, t4, t5, t6, t7, t8);
1
}

            
/// An error occurred inside of one of the composite key fields.
#[derive(thiserror::Error, Debug)]
#[error("key error: {0}")]
pub struct CompositeKeyError(Box<dyn AnyError>);

            
impl CompositeKeyError {
    /// Returns a new instance wrapping `error`.
1
    pub fn new<E: AnyError>(error: E) -> Self {
1
        Self(Box::new(error))
1
    }
}

            
impl From<TryFromIntError> for CompositeKeyError {
    fn from(err: TryFromIntError) -> Self {
        Self::new(err)
    }
}

            
impl From<io::Error> for CompositeKeyError {
    fn from(err: io::Error) -> Self {
        Self::new(err)
    }
}

            
impl<'k> Key<'k> for Signed {
    const CAN_OWN_BYTES: bool = false;

            
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
        Self::decode_variable(bytes.as_ref())
    }

            
    fn first_value() -> Result<Self, NextValueError> {
        Ok(Self::from(0_i128))
    }

            
    fn next_value(&self) -> Result<Self, NextValueError> {
        i128::from(*self)
            .checked_add(1)
            .map(Self::from)
            .ok_or(NextValueError::WouldWrap)
    }
}

            
impl KeyEncoding<Self> for Signed {
    type Error = io::Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Signed);
    }

            
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
        self.to_variable_vec().map(Cow::Owned)
    }
}

            
impl<'k> Key<'k> for Unsigned {
    const CAN_OWN_BYTES: bool = false;

            
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
        Self::decode_variable(bytes.as_ref())
    }

            
    fn first_value() -> Result<Self, NextValueError> {
        Ok(Self::from(0_u128))
    }

            
    fn next_value(&self) -> Result<Self, NextValueError> {
        u128::from(*self)
            .checked_add(1)
            .map(Self::from)
            .ok_or(NextValueError::WouldWrap)
    }
}

            
impl KeyEncoding<Self> for Unsigned {
    type Error = io::Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Unsigned);
    }

            
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
        self.to_variable_vec().map(Cow::Owned)
    }
}

            
impl<'k> Key<'k> for isize {
    const CAN_OWN_BYTES: bool = false;

            
2
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
2
        Self::decode_variable(bytes.as_ref())
2
    }

            
    fn first_value() -> Result<Self, NextValueError> {
        Ok(0)
    }

            
    fn next_value(&self) -> Result<Self, NextValueError> {
        self.checked_add(1).ok_or(NextValueError::WouldWrap)
    }
}

            
impl KeyEncoding<Self> for isize {
    type Error = io::Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Signed);
    }

            
203
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
203
        self.to_variable_vec().map(Cow::Owned)
203
    }
}

            
impl<'k> Key<'k> for usize {
    const CAN_OWN_BYTES: bool = false;

            
2
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
2
        Self::decode_variable(bytes.as_ref())
2
    }

            
    fn first_value() -> Result<Self, NextValueError> {
        Ok(0)
    }

            
    fn next_value(&self) -> Result<Self, NextValueError> {
        self.checked_add(1).ok_or(NextValueError::WouldWrap)
    }
}

            
impl KeyEncoding<Self> for usize {
    type Error = io::Error;

            
    const LENGTH: Option<Self> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Unsigned);
    }

            
3
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
3
        self.to_variable_vec().map(Cow::Owned)
3
    }
}

            
impl<'k> Key<'k> for NonZeroIsize {
    const CAN_OWN_BYTES: bool = false;

            
2
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
2
        let possibly_zero = isize::decode_variable(bytes.as_ref())?;
1
        Self::new(possibly_zero)
1
            .ok_or_else(|| io::Error::new(ErrorKind::InvalidData, NonZeroKeyError::ValueIsZero))
2
    }

            
1
    fn first_value() -> Result<Self, NextValueError> {
1
        Ok(Self::new(1).expect("one is not zero"))
1
    }

            
1
    fn next_value(&self) -> Result<Self, NextValueError> {
1
        let next = self.get().checked_add(1).ok_or(NextValueError::WouldWrap)?;
1
        Ok(Self::new(next)
1
            .unwrap_or_else(|| Self::first_value().expect("first_value returned error")))
1
    }
}

            
impl KeyEncoding<Self> for NonZeroIsize {
    type Error = io::Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Signed);
    }

            
2
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
2
        self.get().to_variable_vec().map(Cow::Owned)
2
    }
}

            
impl<'k> Key<'k> for NonZeroUsize {
    const CAN_OWN_BYTES: bool = false;

            
3
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
3
        let possibly_zero = usize::decode_variable(bytes.as_ref())?;
3
        Self::new(possibly_zero)
3
            .ok_or_else(|| io::Error::new(ErrorKind::InvalidData, NonZeroKeyError::ValueIsZero))
3
    }

            
    fn first_value() -> Result<Self, NextValueError> {
        Ok(Self::new(1).expect("one is not zero"))
    }

            
    fn next_value(&self) -> Result<Self, NextValueError> {
        self.checked_add(1).ok_or(NextValueError::WouldWrap)
    }
}

            
impl KeyEncoding<Self> for NonZeroUsize {
    type Error = io::Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Unsigned);
    }

            
3
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
3
        self.get().to_variable_vec().map(Cow::Owned)
3
    }
}

            
#[cfg(feature = "uuid")]
impl<'k> Key<'k> for uuid::Uuid {
    const CAN_OWN_BYTES: bool = false;

            
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
        Ok(Self::from_bytes(bytes.as_ref().try_into()?))
    }
}

            
#[cfg(feature = "uuid")]
impl KeyEncoding<Self> for uuid::Uuid {
    type Error = std::array::TryFromSliceError;

            
    const LENGTH: Option<usize> = Some(16);

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_composite(CompositeKind::Struct(Cow::Borrowed("uuid::Uuid")), 1);
        visitor.visit_type(KeyKind::Bytes);
    }

            
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
        Ok(Cow::Borrowed(self.as_bytes()))
    }
}

            
13255
fn decode_skipping_first_byte<'k, 'e, T>(bytes: ByteSource<'k, 'e>) -> Result<T, T::Error>
13255
where
13255
    T: Key<'k>,
13255
{
    match bytes {
13255
        ByteSource::Borrowed(bytes) => T::from_ord_bytes(ByteSource::Borrowed(&bytes[1..])),
        ByteSource::Ephemeral(bytes) => T::from_ord_bytes(ByteSource::Ephemeral(&bytes[1..])),
        ByteSource::Owned(mut bytes) if T::CAN_OWN_BYTES => {
            bytes.remove(0);
            T::from_ord_bytes(ByteSource::Owned(bytes))
        }
        ByteSource::Owned(bytes) => T::from_ord_bytes(ByteSource::Ephemeral(&bytes[1..])),
    }
13255
}

            
impl<'k, T> Key<'k> for Option<T>
where
    T: Key<'k>,
    Self: KeyEncoding<Self, Error = <T as KeyEncoding<T>>::Error>,
{
    const CAN_OWN_BYTES: bool = T::CAN_OWN_BYTES;

            
160876652
    fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
160876652
        if bytes.as_ref().is_empty() || bytes.as_ref()[0] == 0 {
160863401
            Ok(None)
        } else {
13251
            decode_skipping_first_byte(bytes).map(Some)
        }
160876652
    }

            
    fn first_value() -> Result<Self, NextValueError> {
        Ok(Some(T::first_value()?))
    }

            
    fn next_value(&self) -> Result<Self, NextValueError> {
        self.as_ref().map(T::next_value).transpose()
    }
}

            
impl<'k, T, K> KeyEncoding<Option<K>> for Option<T>
where
    T: KeyEncoding<K>,
    K: Key<'k>,
{
    type Error = T::Error;

            
    const LENGTH: Option<usize> = match T::LENGTH {
        Some(length) => Some(1 + length),
        None => None,
    };

            
960
    fn describe<Visitor>(visitor: &mut Visitor)
960
    where
960
        Visitor: KeyVisitor,
960
    {
960
        visitor.visit_composite(CompositeKind::Option, 1);
960
        T::describe(visitor);
960
    }

            
507931
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
507931
        if let Some(contents) = self {
13890
            let mut contents = contents.as_ord_bytes()?.to_vec();
13890
            contents.insert(0, 1);
13890
            Ok(Cow::Owned(contents))
        } else {
494041
            Ok(Cow::Borrowed(b"\0"))
        }
507931
    }
}

            
const RESULT_OK: u8 = 0;
const RESULT_ERR: u8 = 1;

            
impl<'k, T, E> Key<'k> for Result<T, E>
where
    T: Key<'k>,
    E: Key<'k, Error = <T as KeyEncoding<T>>::Error>,
    Self: KeyEncoding<Self, Error = <T as KeyEncoding<T>>::Error>,
{
    const CAN_OWN_BYTES: bool = T::CAN_OWN_BYTES || E::CAN_OWN_BYTES;

            
4
    fn from_ord_bytes<'b>(bytes: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
4
        match bytes.as_ref().first() {
2
            Some(&RESULT_OK) => decode_skipping_first_byte(bytes).map(Ok),
2
            Some(_) => decode_skipping_first_byte(bytes).map(Err),
            None => {
                // Empty buffer, but we don't have an error type.
                E::from_ord_bytes(bytes).map(Err)
            }
        }
4
    }

            
1
    fn first_value() -> Result<Self, NextValueError> {
1
        Ok(Ok(T::first_value()?))
1
    }

            
2
    fn next_value(&self) -> Result<Self, NextValueError> {
2
        match self {
1
            Ok(value) => value.next_value().map(Ok),
1
            Err(err) => err.next_value().map(Err),
        }
2
    }
}

            
impl<'k, T, E, TBorrowed, EBorrowed> KeyEncoding<Result<T, E>> for Result<TBorrowed, EBorrowed>
where
    TBorrowed: KeyEncoding<T>,
    T: Key<'k, Error = TBorrowed::Error>,
    EBorrowed: KeyEncoding<E, Error = TBorrowed::Error>,
    E: Key<'k, Error = TBorrowed::Error>,
{
    type Error = <TBorrowed as KeyEncoding<T>>::Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_composite(CompositeKind::Result, 2);
        TBorrowed::describe(visitor);
        EBorrowed::describe(visitor);
    }

            
4
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
4
        let (header, contents) = match self {
2
            Ok(value) => (RESULT_OK, value.as_ord_bytes()?),
2
            Err(value) => (RESULT_ERR, value.as_ord_bytes()?),
        };
4
        let mut contents = contents.to_vec();
4
        contents.insert(0, header);
4
        Ok(Cow::Owned(contents))
4
    }
}

            
1
#[test]
1
fn result_key_tests() {
1
    let ok_0 = Result::<u8, u16>::first_value().unwrap();
1
    let ok_1 = ok_0.next_value().unwrap();
1
    assert_eq!(ok_1, Ok(1));
1
    let ok_2 = Result::<u8, u16>::Ok(2_u8);
1
    let err_1 = Result::<u8, u16>::Err(1_u16);
1
    let err_2 = err_1.next_value().unwrap();
1
    assert_eq!(err_2, Err(2));
1
    let ok_1_encoded = ok_1.as_ord_bytes().unwrap();
1
    let ok_2_encoded = ok_2.as_ord_bytes().unwrap();
1
    let err_1_encoded = err_1.as_ord_bytes().unwrap();
1
    let err_2_encoded = err_2.as_ord_bytes().unwrap();
1
    assert!(ok_1_encoded < ok_2_encoded);
1
    assert!(ok_2_encoded < err_1_encoded);
1
    assert!(err_1_encoded < err_2_encoded);
1
    assert_eq!(
1
        Result::from_ord_bytes(ByteSource::Borrowed(&ok_1_encoded)).unwrap(),
1
        ok_1
1
    );
1
    assert_eq!(
1
        Result::from_ord_bytes(ByteSource::Borrowed(&ok_2_encoded)).unwrap(),
1
        ok_2
1
    );
1
    assert_eq!(
1
        Result::from_ord_bytes(ByteSource::Borrowed(&err_1_encoded)).unwrap(),
1
        err_1
1
    );
1
    assert_eq!(
1
        Result::from_ord_bytes(ByteSource::Borrowed(&err_2_encoded)).unwrap(),
1
        err_2
1
    );
1
}

            
/// Adds `Key` support to an enum. Requires implementing
/// [`ToPrimitive`](num_traits::ToPrimitive) and
/// [`FromPrimitive`](num_traits::FromPrimitive), or using a crate like
/// [num-derive](https://crates.io/crates/num-derive) to do it automatically.
/// Take care when using enums as keys: if the order changes or if the meaning
/// of existing numerical values changes, make sure to update any related views'
/// version number to ensure the values are re-evaluated.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub struct EnumKey<T>(pub T)
where
    T: ToPrimitive + FromPrimitive + Clone + Eq + Ord + std::fmt::Debug + Send + Sync;

            
/// An error that indicates an unexpected number of bytes were present.
#[derive(thiserror::Error, Debug)]
#[error("incorrect byte length")]
pub struct IncorrectByteLength;

            
/// An error that indicates an unexpected enum variant value was found.
#[derive(thiserror::Error, Debug)]
#[error("unknown enum variant")]
pub struct UnknownEnumVariant;

            
impl From<std::array::TryFromSliceError> for IncorrectByteLength {
    fn from(_: std::array::TryFromSliceError) -> Self {
        Self
    }
}

            
// ANCHOR: impl_key_for_enumkey
impl<'k, T> Key<'k> for EnumKey<T>
where
    T: ToPrimitive + FromPrimitive + Clone + Eq + Ord + std::fmt::Debug + Send + Sync,
{
    const CAN_OWN_BYTES: bool = false;

            
2
    fn from_ord_bytes<'b>(bytes: ByteSource<'k, 'b>) -> Result<Self, Self::Error> {
2
        let primitive = u64::decode_variable(bytes.as_ref())?;
2
        T::from_u64(primitive)
2
            .map(Self)
2
            .ok_or_else(|| io::Error::new(ErrorKind::InvalidData, UnknownEnumVariant))
2
    }
}

            
impl<T> KeyEncoding<Self> for EnumKey<T>
where
    T: ToPrimitive + FromPrimitive + Clone + Eq + Ord + std::fmt::Debug + Send + Sync,
{
    type Error = io::Error;

            
    const LENGTH: Option<usize> = None;

            
    fn describe<Visitor>(visitor: &mut Visitor)
    where
        Visitor: KeyVisitor,
    {
        visitor.visit_type(KeyKind::Unsigned);
    }

            
2
    fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
2
        let integer = self
2
            .0
2
            .to_u64()
2
            .map(Unsigned::from)
2
            .ok_or_else(|| io::Error::new(ErrorKind::InvalidData, IncorrectByteLength))?;
2
        Ok(Cow::Owned(integer.to_variable_vec()?))
2
    }
}
// ANCHOR_END: impl_key_for_enumkey

            
macro_rules! impl_key_for_primitive {
    ($type:ident, $keykind:expr) => {
        impl<'k> Key<'k> for $type {
            const CAN_OWN_BYTES: bool = false;

            
4370158
            fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
4370158
                Ok($type::from_be_bytes(bytes.as_ref().try_into()?))
4370158
            }

            
44161
            fn first_value() -> Result<Self, NextValueError> {
44161
                Ok(0)
44161
            }

            
670802
            fn next_value(&self) -> Result<Self, NextValueError> {
670802
                self.checked_add(1).ok_or(NextValueError::WouldWrap)
670802
            }
        }
        impl KeyEncoding<Self> for $type {
            type Error = IncorrectByteLength;

            
            const LENGTH: Option<usize> = Some(std::mem::size_of::<$type>());

            
5834259
            fn describe<Visitor>(visitor: &mut Visitor)
5834259
            where
5834259
                Visitor: KeyVisitor,
5834259
            {
5834259
                visitor.visit_type($keykind);
5834259
            }

            
4081169
            fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
4081169
                Ok(Cow::from(self.to_be_bytes().to_vec()))
4081169
            }
        }
    };
}

            
impl_key_for_primitive!(i8, KeyKind::I8);
impl_key_for_primitive!(u8, KeyKind::U8);
impl_key_for_primitive!(i16, KeyKind::I16);
impl_key_for_primitive!(u16, KeyKind::U16);
impl_key_for_primitive!(i32, KeyKind::I32);
impl_key_for_primitive!(u32, KeyKind::U32);
impl_key_for_primitive!(i64, KeyKind::I64);
impl_key_for_primitive!(u64, KeyKind::U64);
impl_key_for_primitive!(i128, KeyKind::I128);
impl_key_for_primitive!(u128, KeyKind::U128);

            
macro_rules! impl_key_for_nonzero_primitive {
    ($nonzero:ident, $type:ident) => {
        impl<'k> Key<'k> for $nonzero {
            const CAN_OWN_BYTES: bool = false;

            
30
            fn from_ord_bytes<'e>(bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
30
                let contents = $type::from_be_bytes(bytes.as_ref().try_into()?);
30
                $nonzero::new(contents).ok_or(NonZeroKeyError::ValueIsZero)
30
            }

            
5
            fn first_value() -> Result<Self, NextValueError> {
5
                Ok($nonzero::new(1).expect("one is not zero"))
5
            }

            
5
            fn next_value(&self) -> Result<Self, NextValueError> {
                if let Some(nonzero) =
5
                    $nonzero::new(self.get().checked_add(1).ok_or(NextValueError::WouldWrap)?)
                {
                    Ok(nonzero)
                } else {
                    // Since we've done a checked_add, we know the only failure
                    // case here is going from negative to positive. In this
                    // sitaution, we skip 0.
5
                    Self::first_value()
                }
5
            }
        }

            
        impl KeyEncoding<Self> for $nonzero {
            type Error = NonZeroKeyError;

            
            const LENGTH: Option<usize> = Some(std::mem::size_of::<$type>());

            
            fn describe<Visitor>(visitor: &mut Visitor)
            where
                Visitor: KeyVisitor,
            {
                $type::describe(visitor);
            }

            
30
            fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
30
                Ok(Cow::from(self.get().to_be_bytes().to_vec()))
30
            }
        }
    };
}

            
impl_key_for_nonzero_primitive!(NonZeroI8, i8);
impl_key_for_nonzero_primitive!(NonZeroU8, u8);
impl_key_for_nonzero_primitive!(NonZeroI16, i16);
impl_key_for_nonzero_primitive!(NonZeroU16, u16);
impl_key_for_nonzero_primitive!(NonZeroI32, i32);
impl_key_for_nonzero_primitive!(NonZeroU32, u32);
impl_key_for_nonzero_primitive!(NonZeroI64, i64);
impl_key_for_nonzero_primitive!(NonZeroU64, u64);
impl_key_for_nonzero_primitive!(NonZeroI128, i128);
impl_key_for_nonzero_primitive!(NonZeroU128, u128);

            
/// An error occurred during a [`Key`] or [`KeyEncoding`] implementation for a
/// non-zero type.
#[derive(thiserror::Error, Debug)]
pub enum NonZeroKeyError {
    /// An incorrect number of bytes were encounted for the type specified.
    #[error("incorrect byte length")]
    IncorrectByteLength,
    /// A zero value was encountered for a non-zero type.
    #[error("zero is not valid for a non-zero type")]
    ValueIsZero,
}

            
impl From<std::array::TryFromSliceError> for NonZeroKeyError {
    fn from(_value: std::array::TryFromSliceError) -> Self {
        Self::IncorrectByteLength
    }
}

            
1
#[test]
#[allow(clippy::cognitive_complexity)] // I disagree - @ecton
1
fn primitive_key_encoding_tests() -> anyhow::Result<()> {
1
    macro_rules! test_primitive_extremes {
1
        ($type:ident) => {
1
            assert_eq!(
1
                &$type::MAX.to_be_bytes(),
1
                $type::MAX.as_ord_bytes()?.as_ref()
1
            );
1
            assert_eq!(
1
                $type::MAX,
1
                $type::from_ord_bytes(ByteSource::Borrowed(&$type::MAX.as_ord_bytes()?))?
1
            );
1
            assert_eq!(
1
                $type::MIN,
1
                $type::from_ord_bytes(ByteSource::Borrowed(&$type::MIN.as_ord_bytes()?))?
1
            );
1
        };
1
    }
1

            
1
    test_primitive_extremes!(i8);
1
    test_primitive_extremes!(u8);
1
    test_primitive_extremes!(i16);
1
    test_primitive_extremes!(u16);
1
    test_primitive_extremes!(i32);
1
    test_primitive_extremes!(u32);
1
    test_primitive_extremes!(i64);
1
    test_primitive_extremes!(u64);
1
    test_primitive_extremes!(i128);
1
    test_primitive_extremes!(u128);

            
1
    assert_eq!(
1
        usize::from_ord_bytes(ByteSource::Borrowed(&usize::MAX.as_ord_bytes().unwrap())).unwrap(),
1
        usize::MAX
1
    );
1
    assert_eq!(
1
        usize::from_ord_bytes(ByteSource::Borrowed(&usize::MIN.as_ord_bytes().unwrap())).unwrap(),
1
        usize::MIN
1
    );
1
    assert_eq!(
1
        isize::from_ord_bytes(ByteSource::Borrowed(&isize::MAX.as_ord_bytes().unwrap())).unwrap(),
1
        isize::MAX
1
    );
1
    assert_eq!(
1
        isize::from_ord_bytes(ByteSource::Borrowed(&isize::MIN.as_ord_bytes().unwrap())).unwrap(),
1
        isize::MIN
1
    );

            
1
    Ok(())
1
}

            
1
#[test]
1
fn nonzero_key_encoding_tests() -> anyhow::Result<()> {
1
    macro_rules! test_nonzero {
1
        ($nonzero:ident, $inner:ident) => {
1
            let zero_bytes = [0; ($inner::BITS / 8) as usize];
1
            assert!(matches!(
1
                $nonzero::from_ord_bytes(ByteSource::Borrowed(&zero_bytes)),
1
                Err(NonZeroKeyError::ValueIsZero)
1
            ));
1
            let max = $nonzero::new($inner::MAX).unwrap();
1
            assert_eq!(&max.get().to_be_bytes(), max.as_ord_bytes()?.as_ref());
1
            assert_eq!(
1
                max,
1
                $nonzero::from_ord_bytes(ByteSource::Borrowed(&max.as_ord_bytes()?))?
1
            );
1
            let min = $nonzero::new(if $inner::MIN == 0 { 1 } else { $inner::MIN }).unwrap();
1
            assert_eq!(
1
                min,
1
                $nonzero::from_ord_bytes(ByteSource::Borrowed(&min.as_ord_bytes()?))?
1
            );
1
        };
1
    }
1
    macro_rules! test_signed {
1
        ($nonzero:ident, $inner:ident) => {
1
            test_nonzero!($nonzero, $inner);
1
            let negative_one = $nonzero::new(-1).unwrap();
1
            assert_eq!(negative_one.next_value().unwrap().get(), 1);
1
        };
1
    }
1

            
1
    test_nonzero!(NonZeroU8, u8);
1
    test_nonzero!(NonZeroU16, u16);
1
    test_nonzero!(NonZeroU32, u32);
1
    test_nonzero!(NonZeroU64, u64);
1
    test_nonzero!(NonZeroU128, u128);
1
    test_signed!(NonZeroI8, i8);
1
    test_signed!(NonZeroI16, i16);
1
    test_signed!(NonZeroI32, i32);
1
    test_signed!(NonZeroI64, i64);
1
    test_signed!(NonZeroI128, i128);

            
    // NonZeroUsize
1
    let zero_bytes = [0; (usize::BITS / 8) as usize];
1
    assert!(NonZeroUsize::from_ord_bytes(ByteSource::Borrowed(&zero_bytes)).is_err());
1
    let max = NonZeroUsize::new(usize::MAX).unwrap();
1
    assert_eq!(
1
        max.get().as_ord_bytes()?.as_ref(),
1
        max.as_ord_bytes()?.as_ref()
    );
1
    assert_eq!(
1
        max,
1
        NonZeroUsize::from_ord_bytes(ByteSource::Borrowed(&max.as_ord_bytes()?))?
    );
1
    let min = NonZeroUsize::new(1).unwrap();
1
    assert_eq!(
1
        min,
1
        NonZeroUsize::from_ord_bytes(ByteSource::Borrowed(&min.as_ord_bytes()?))?
    );

            
    // NonZeroIsize
1
    assert!(NonZeroIsize::from_ord_bytes(ByteSource::Borrowed(&zero_bytes)).is_err());
1
    let max = NonZeroIsize::new(isize::MAX).unwrap();
1
    assert_eq!(max.get().as_ord_bytes()?, max.as_ord_bytes()?.as_ref());
1
    let min = NonZeroIsize::new(isize::MIN).unwrap();
1
    assert_eq!(
1
        min,
1
        NonZeroIsize::from_ord_bytes(ByteSource::Borrowed(&min.as_ord_bytes()?))?
    );
1
    let negative_one = NonZeroIsize::new(-1).unwrap();
1
    assert_eq!(negative_one.next_value().unwrap().get(), 1);

            
1
    Ok(())
1
}

            
/// A Transmog [`Format`](transmog::Format) implementation that uses [`Key`] to
/// serialize and deserialize the contents.
2
#[derive(Default, Debug)]
pub struct KeyFormat;

            
impl KeyFormat {
1
    fn get_bytes<T: KeyEncoding>(value: &T) -> io::Result<Cow<'_, [u8]>> {
1
        value
1
            .as_ord_bytes()
1
            .map_err(|err| io::Error::new(ErrorKind::InvalidInput, err))
1
    }
}

            
impl<'a, T> transmog::Format<'a, T> for KeyFormat
where
    T: KeyEncoding,
{
    type Error = io::Error;

            
    fn serialize_into<W: io::Write>(&self, value: &T, mut writer: W) -> Result<(), Self::Error> {
        let data = Self::get_bytes(value)?;
        writer.write_all(&data)
    }

            
    fn serialized_size(&self, _value: &T) -> Result<Option<usize>, Self::Error> {
        Ok(T::LENGTH)
    }

            
1
    fn serialize(&self, value: &T) -> Result<Vec<u8>, Self::Error> {
1
        Ok(Self::get_bytes(value)?.into_owned())
1
    }
}

            
impl<'a, T> transmog::BorrowedDeserializer<'a, T> for KeyFormat
where
    T: Key<'a>,
{
1
    fn deserialize_borrowed(&self, data: &'a [u8]) -> Result<T, Self::Error> {
1
        T::from_ord_bytes(ByteSource::Borrowed(data))
1
            .map_err(|err| io::Error::new(ErrorKind::InvalidData, err))
1
    }
}

            
impl<T> transmog::OwnedDeserializer<T> for KeyFormat
where
    T: for<'a> Key<'a> + 'static,
{
    fn deserialize_from<R: io::Read>(&self, mut reader: R) -> Result<T, Self::Error> {
        let mut buffer = Vec::new();
        reader.read_to_end(&mut buffer)?;
        self.deserialize_owned(&buffer)
    }

            
1
    fn deserialize_owned(&self, data: &[u8]) -> Result<T, Self::Error> {
1
        let possibly_borrowed = self.deserialize_borrowed(data)?;
1
        Ok(possibly_borrowed)
1
    }
}

            
1
#[test]
1
fn optional_key_encoding_tests() -> anyhow::Result<()> {
1
    let some_string = Some("hello").as_ord_bytes()?;
1
    let empty_string = Some("").as_ord_bytes()?;
1
    let none_string = Option::<String>::None.as_ord_bytes()?;
1
    assert_eq!(
1
        Option::<String>::from_ord_bytes(ByteSource::Borrowed(&some_string))
1
            .unwrap()
1
            .as_deref(),
1
        Some("hello")
1
    );
1
    assert_eq!(
1
        Option::<String>::from_ord_bytes(ByteSource::Borrowed(&empty_string))
1
            .unwrap()
1
            .as_deref(),
1
        Some("")
1
    );
1
    assert_eq!(
1
        Option::<String>::from_ord_bytes(ByteSource::Borrowed(&none_string)).unwrap(),
1
        None
1
    );

            
    #[allow(deprecated)]
    {
1
        let some_string = OptionKeyV1(Some("hello")).as_ord_bytes()?.to_vec();
1
        let none_string = OptionKeyV1::<String>(None).as_ord_bytes()?;
1
        assert_eq!(
1
            OptionKeyV1::<String>::from_ord_bytes(ByteSource::Borrowed(&some_string))
1
                .unwrap()
1
                .0
1
                .as_deref(),
1
            Some("hello")
1
        );
1
        assert_eq!(
1
            OptionKeyV1::<String>::from_ord_bytes(ByteSource::Borrowed(&none_string))
1
                .unwrap()
1
                .0,
1
            None
1
        );
    }
1
    Ok(())
1
}

            
1
#[test]
#[allow(clippy::unit_cmp)] // this is more of a compilation test
1
fn unit_key_encoding_tests() -> anyhow::Result<()> {
1
    assert!(().as_ord_bytes()?.is_empty());
1
    assert_eq!((), <() as Key>::from_ord_bytes(ByteSource::Borrowed(&[]))?);
1
    Ok(())
1
}

            
1
#[test]
#[allow(clippy::unit_cmp)] // this is more of a compilation test
1
fn bool_key_encoding_tests() -> anyhow::Result<()> {
1
    let true_as_bytes = true.as_ord_bytes()?;
1
    let false_as_bytes = false.as_ord_bytes()?;
1
    assert!(bool::from_ord_bytes(ByteSource::Borrowed(&true_as_bytes))?);
1
    assert!(!bool::from_ord_bytes(ByteSource::Borrowed(
1
        &false_as_bytes
1
    ))?);
1
    Ok(())
1
}

            
1
#[test]
1
fn vec_key_encoding_tests() -> anyhow::Result<()> {
1
    const ORIGINAL_VALUE: &[u8] = b"bonsaidb";
1
    let vec = Cow::<'_, [u8]>::from(ORIGINAL_VALUE);
1
    assert_eq!(
1
        vec.clone(),
1
        Cow::<'_, [u8]>::from_ord_bytes(ByteSource::Borrowed(&vec.as_ord_bytes()?))?
    );
1
    Ok(())
1
}

            
1
#[test]
1
fn enum_derive_tests() -> anyhow::Result<()> {
    #[derive(
        Debug,
        Clone,
4
        num_derive::ToPrimitive,
2
        num_derive::FromPrimitive,
        Ord,
        PartialOrd,
        Eq,
        PartialEq,
    )]
    enum SomeEnum {
        One = 1,
        NineNineNine = 999,
    }

            
1
    let encoded = EnumKey(SomeEnum::One).as_ord_bytes()?;
1
    let value = EnumKey::<SomeEnum>::from_ord_bytes(ByteSource::Borrowed(&encoded))?;
1
    assert!(matches!(value.0, SomeEnum::One));

            
1
    let encoded = EnumKey(SomeEnum::NineNineNine).as_ord_bytes()?;
1
    let value = EnumKey::<SomeEnum>::from_ord_bytes(ByteSource::Borrowed(&encoded))?;
1
    assert!(matches!(value.0, SomeEnum::NineNineNine));

            
1
    Ok(())
1
}

            
1
#[test]
1
fn key_descriptions() {
1
    use time::limited::TimeEpoch;
1

            
1
    #[derive(Clone)]
1
    struct NoDescriptionKey;
1

            
1
    impl<'k> Key<'k> for NoDescriptionKey {
1
        const CAN_OWN_BYTES: bool = false;
1

            
1
        fn from_ord_bytes<'e>(_bytes: ByteSource<'k, 'e>) -> Result<Self, Self::Error> {
            Ok(NoDescriptionKey)
        }
1
    }
1

            
1
    impl KeyEncoding<NoDescriptionKey> for NoDescriptionKey {
1
        type Error = Infallible;
1

            
1
        const LENGTH: Option<usize> = None;
1

            
1
        fn describe<Visitor>(_visitor: &mut Visitor)
1
        where
1
            Visitor: KeyVisitor,
1
        {
1
        }
1

            
1
        fn as_ord_bytes(&self) -> Result<Cow<'_, [u8]>, Self::Error> {
            Ok(Cow::Borrowed(b""))
        }
1
    }
1

            
1
    assert_eq!(
1
        KeyDescription::for_key::<Vec<u8>>(),
1
        KeyDescription::Basic(KeyKind::Bytes)
1
    );
1
    assert_eq!(
1
        dbg!(KeyDescription::for_key::<time::TimestampAsNanoseconds>()),
1
        KeyDescription::Composite(CompositeKeyDescription {
1
            kind: CompositeKind::Struct(Cow::Borrowed(
1
                "bonsaidb::core::key::time::LimitedResolutionTimestamp"
1
            )),
1
            fields: vec![KeyDescription::Basic(KeyKind::I64),],
1
            attributes: [(
1
                Cow::Borrowed("epoch"),
1
                KeyAttibuteValue::U128(time::limited::BonsaiEpoch::epoch_offset().as_nanos())
1
            )]
1
            .into_iter()
1
            .collect()
1
        })
1
    );
1
    assert_eq!(
1
        KeyDescription::for_key::<(u64, String, Bytes)>(),
1
        KeyDescription::Composite(CompositeKeyDescription {
1
            kind: CompositeKind::Tuple,
1
            fields: vec![
1
                KeyDescription::Basic(KeyKind::U64),
1
                KeyDescription::Basic(KeyKind::String),
1
                KeyDescription::Basic(KeyKind::Bytes),
1
            ],
1
            attributes: HashMap::new(),
1
        })
1
    );

            
1
    assert_eq!(
1
        KeyDescription::for_key::<NoDescriptionKey>(),
1
        KeyDescription::Other(Cow::Borrowed(
1
            "bonsaidb_core::key::key_descriptions::NoDescriptionKey"
1
        ))
1
    );
1
}

            
1
#[test]
1
fn key_serialization() {
2
    #[derive(crate::schema::Collection, Key, Clone, Eq, PartialEq, Debug)]
1
    #[collection(core = crate, name = "test", serialization = Key)]
1
    #[key(core = crate)]
1
    struct Test {
1
        field: u64,
1
    }
1
    let start = Test { field: 42 };
1
    let encoded = <Test as crate::schema::SerializedCollection>::serialize(&start).unwrap();
1
    assert_eq!(encoded, &[0, 0, 0, 0, 0, 0, 0, 42]);
1
    let decoded = <Test as crate::schema::SerializedCollection>::deserialize(&encoded).unwrap();
1
    assert_eq!(decoded, start);
1
}