extern crate alloc;
use super::{ColumnError, ColumnReader, ColumnWriter};
pub trait Field: Sized {
fn write(&self, w: &mut impl ColumnWriter);
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error>;
}
macro_rules! row_integer {
($($ty:ident),* $(,)?) => {
$(impl Field for $ty {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.$ty(*self);
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
r.$ty()
}
})*
};
}
row_integer!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
macro_rules! row_nonzero {
($($ty:ty => $int:ident),* $(,)?) => {
$(impl Field for $ty {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.$int(self.get());
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
let raw = r.$int()?;
Self::new(raw).ok_or_else(|| R::Error::domain(raw))
}
})*
};
}
row_nonzero!(
core::num::NonZeroI8 => i8,
core::num::NonZeroI16 => i16,
core::num::NonZeroI32 => i32,
core::num::NonZeroI64 => i64,
core::num::NonZeroI128 => i128,
core::num::NonZeroU8 => u8,
core::num::NonZeroU16 => u16,
core::num::NonZeroU32 => u32,
core::num::NonZeroU64 => u64,
core::num::NonZeroU128 => u128,
);
impl Field for uuid::Uuid {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.uuid(*self);
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
r.uuid()
}
}
macro_rules! row_text {
($($ty:ty),* $(,)?) => {
$(impl Field for $ty {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.text(self.as_ref());
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(Self::from(r.text()?))
}
})*
};
}
row_text!(String);
impl Field for smol_bytes::Utf8Bytes {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.text(self.as_ref());
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(Self::from(r.text()?.to_owned()))
}
}
#[cfg(feature = "smol_str")]
row_text!(smol_str::SmolStr);
impl Field for core::net::Ipv4Addr {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(&self.octets());
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(Self::from(r.fixed::<4>()?))
}
}
impl Field for core::net::Ipv6Addr {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(&self.octets());
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(Self::from(r.fixed::<16>()?))
}
}
impl Field for core::net::IpAddr {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
match self {
Self::V4(host) => w.bytes(&host.octets()),
Self::V6(host) => w.bytes(&host.octets()),
}
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
let value = r.bytes()?;
match value.len() {
4 => Ok(Self::V4(core::net::Ipv4Addr::from(
<[u8; 4]>::try_from(value).expect("a column this arm measured at four bytes"),
))),
16 => Ok(Self::V6(core::net::Ipv6Addr::from(
<[u8; 16]>::try_from(value).expect("a column this arm measured at sixteen bytes"),
))),
got => Err(R::Error::width(got, &[4, 16])),
}
}
}
const PORT: usize = 2;
const V4: usize = 4 + PORT;
const V6: usize = 16 + PORT + 4 + 4;
fn pack_v4(host: &core::net::SocketAddrV4) -> [u8; V4] {
let mut value = [0u8; V4];
value[..4].copy_from_slice(&host.ip().octets());
value[4..].copy_from_slice(&host.port().to_be_bytes());
value
}
fn socket_v4(value: [u8; V4]) -> core::net::SocketAddrV4 {
let (octets, port) = value.split_at(4);
core::net::SocketAddrV4::new(
core::net::Ipv4Addr::from(<[u8; 4]>::try_from(octets).expect("a 4-byte slice")),
u16::from_be_bytes(<[u8; PORT]>::try_from(port).expect("a 2-byte slice")),
)
}
fn pack_v6(host: &core::net::SocketAddrV6) -> [u8; V6] {
let mut value = [0u8; V6];
value[..16].copy_from_slice(&host.ip().octets());
value[16..18].copy_from_slice(&host.port().to_be_bytes());
value[18..22].copy_from_slice(&host.flowinfo().to_be_bytes());
value[22..].copy_from_slice(&host.scope_id().to_be_bytes());
value
}
fn socket_v6(value: [u8; V6]) -> core::net::SocketAddrV6 {
let word = |at: usize| {
u32::from_be_bytes(<[u8; 4]>::try_from(&value[at..at + 4]).expect("a 4-byte slice"))
};
core::net::SocketAddrV6::new(
core::net::Ipv6Addr::from(<[u8; 16]>::try_from(&value[..16]).expect("a 16-byte slice")),
u16::from_be_bytes(<[u8; PORT]>::try_from(&value[16..18]).expect("a 2-byte slice")),
word(18),
word(22),
)
}
impl Field for core::net::SocketAddrV4 {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(&pack_v4(self));
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(socket_v4(r.fixed()?))
}
}
impl Field for core::net::SocketAddrV6 {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(&pack_v6(self));
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(socket_v6(r.fixed()?))
}
}
impl Field for core::net::SocketAddr {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
match self {
Self::V4(host) => w.bytes(&pack_v4(host)),
Self::V6(host) => w.bytes(&pack_v6(host)),
}
}
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
let value = r.bytes()?;
match value.len() {
V4 => Ok(Self::V4(socket_v4(
<[u8; V4]>::try_from(value).expect("a column this arm measured at six bytes"),
))),
V6 => Ok(Self::V6(socket_v6(
<[u8; V6]>::try_from(value).expect("a column this arm measured at twenty-six bytes"),
))),
got => Err(R::Error::width(got, &[V4, V6])),
}
}
}
impl<const N: usize> Field for [u8; N] {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(self);
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
r.fixed::<N>()
}
}
macro_rules! row_pointer {
($($ty:ty),* $(,)?) => {
$(impl<T: Field> Field for $ty {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
T::write(self, w);
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
T::read(r).map(Self::new)
}
})*
};
}
row_pointer!(alloc::sync::Arc<T>, alloc::rc::Rc<T>, alloc::boxed::Box<T>);
impl<I: Field, E> Field for crate::TypedId<I, E> {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
self.id_ref().write(w);
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
I::read(r).map(Self::new)
}
}
#[cfg(feature = "mongo")]
impl Field for bson::Uuid {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(&self.bytes());
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(Self::from_bytes(r.fixed::<16>()?))
}
}
#[cfg(feature = "mongo")]
impl Field for bson::oid::ObjectId {
#[inline]
fn write(&self, w: &mut impl ColumnWriter) {
w.fixed(&self.bytes());
}
#[inline]
fn read<R: ColumnReader>(r: &mut R) -> Result<Self, R::Error> {
Ok(Self::from_bytes(r.fixed::<12>()?))
}
}
fn crosses_into_any_engine<F: Field>() {}
macro_rules! audit_row {
($($ty:ty),* $(,)?) => {
$(const _: fn() = crosses_into_any_engine::<$ty>;)*
};
}
crate::id::for_each_id_member!(audit_row);
const _: () = {
struct Witness;
const _: fn() = crosses_into_any_engine::<alloc::sync::Arc<u64>>;
const _: fn() = crosses_into_any_engine::<alloc::rc::Rc<u64>>;
const _: fn() = crosses_into_any_engine::<alloc::boxed::Box<u64>>;
const _: fn() = crosses_into_any_engine::<[u8; 12]>;
const _: fn() = crosses_into_any_engine::<crate::TypedId<uuid::Uuid, Witness>>;
};
#[cfg(test)]
mod tests;