diff --git a/database/src/key.rs b/database/src/key.rs index c0fee09d..69beff0d 100644 --- a/database/src/key.rs +++ b/database/src/key.rs @@ -4,6 +4,8 @@ #[allow(unused_imports)] // docs use crate::table::Table; +use crate::pod::Pod; + //---------------------------------------------------------------------------------------------------- Table /// Database [`Table`] key metadata. /// @@ -18,14 +20,28 @@ pub trait Key { /// will just be the same type as [`Key::Primary`]. const DUPLICATE: bool; - /// The primary key type. - type Primary; + // TODO: fix this sanakirja bound. + cfg_if::cfg_if! { + if #[cfg(all(feature = "sanakirja", not(feature = "heed")))] { + /// The primary key type. + type Primary: Pod + sanakirja::Storable; - /// The secondary key type. - /// - /// Only needs to be different than [`Key::Primary`] - /// if [`Key::DUPLICATE`] is `true`. - type Secondary; + /// The secondary key type. + /// + /// Only needs to be different than [`Key::Primary`] + /// if [`Key::DUPLICATE`] is `true`. + type Secondary: Pod + sanakirja::Storable; + } else { + /// The primary key type. + type Primary: Pod; + + /// The secondary key type. + /// + /// Only needs to be different than [`Key::Primary`] + /// if [`Key::DUPLICATE`] is `true`. + type Secondary: Pod; + } + } /// Acquire [`Key::Primary`]. fn primary(self) -> Self::Primary; @@ -105,23 +121,24 @@ impl_key! { // Implement `Key` for any [`DupKey`] using [`Copy`] types. impl Key for DupKey where - P: Key + Copy, - S: Key + Copy, + // TODO: fix sanakirja serde bound. + P: Pod + Copy, + S: Pod + Copy, { const DUPLICATE: bool = true; - type Primary = Self; + type Primary = P; type Secondary = S; #[inline] fn primary(self) -> Self::Primary { - self + self.primary } #[inline] fn primary_secondary(self) -> (Self::Primary, Self::Secondary) { - (self, self.secondary) + (self.primary, self.secondary) } } diff --git a/database/src/pod.rs b/database/src/pod.rs index 5dc9c7b6..b7c1ed7d 100644 --- a/database/src/pod.rs +++ b/database/src/pod.rs @@ -111,6 +111,7 @@ mod private { impl Sealed for [u8; N] {} impl_sealed! { + std::convert::Infallible, Vec, Box<[u8]>, std::sync::Arc<[u8]>, @@ -132,6 +133,42 @@ mod private { } //---------------------------------------------------------------------------------------------------- Pod Impl (bytes) +// Implement for `Infallible`. +// This type is `!` and should never be constructable, +// so all these functions will just panic. +impl Pod for std::convert::Infallible { + #[cold] + #[inline(never)] + fn as_bytes(&self) -> impl AsRef<[u8]> { + let bytes: &[u8] = unreachable!(); + bytes + } + + #[cold] + #[inline(never)] + fn into_bytes(self) -> Cow<'static, [u8]> { + unreachable!() + } + + #[cold] + #[inline(never)] + fn from_bytes(bytes: &[u8]) -> Self { + unreachable!() + } + + #[cold] + #[inline(never)] + fn from_reader(reader: &mut R) -> Self { + unreachable!() + } + + #[cold] + #[inline(never)] + fn to_writer(self, writer: &mut W) -> usize { + unreachable!() + } +} + // Implement for owned `Vec` bytes. impl Pod for Vec { #[inline] diff --git a/database/src/table.rs b/database/src/table.rs index b500dfdd..c44742fe 100644 --- a/database/src/table.rs +++ b/database/src/table.rs @@ -24,18 +24,15 @@ pub trait Table { /// Whether the table's values are all the same size or not. const CONSTANT_SIZE: bool; + /// Primary key type. + type Key: Key; + // TODO: fix this sanakirja bound. cfg_if::cfg_if! { if #[cfg(all(feature = "sanakirja", not(feature = "heed")))] { - /// Primary key type. - type Key: Key + Pod + sanakirja::Storable; - /// Value type. type Value: Pod + sanakirja::Storable; } else { - /// Primary key type. - type Key: Key + Pod; - /// Value type. type Value: Pod; }