use core::marker::PhantomData;
use super::bridge::Child;
use super::{Table, IMPLICIT, OPTIONAL, REPEATED};
use crate::alloc::vec::Vec;
use crate::{EnumValue, Enumeration, Message, MessageField, ProtoBox};
pub trait MsgSlot {
type Msg;
fn place(&mut self) -> &mut Self::Msg;
fn get(&self) -> Option<&Self::Msg>;
}
impl<T: Default, P: ProtoBox<T>> MsgSlot for MessageField<T, P> {
type Msg = T;
#[inline]
fn place(&mut self) -> &mut T {
self.get_or_insert_default()
}
#[inline]
fn get(&self) -> Option<&T> {
self.as_option()
}
}
pub struct MsgVt {
pub(super) child: Child,
pub(super) place: unsafe fn(*mut u8) -> *mut u8,
pub(super) get: unsafe fn(*const u8) -> *const u8,
pub(super) direct: bool,
}
unsafe fn place_impl<F: MsgSlot>(slot: *mut u8) -> *mut u8 {
unsafe { (*slot.cast::<F>()).place() as *mut F::Msg as *mut u8 }
}
unsafe fn get_impl<F: MsgSlot>(slot: *const u8) -> *const u8 {
match unsafe { (*slot.cast::<F>()).get() } {
Some(m) => (m as *const F::Msg).cast::<u8>(),
None => core::ptr::null(),
}
}
unsafe fn direct_place(slot: *mut u8) -> *mut u8 {
slot
}
unsafe fn direct_get(slot: *const u8) -> *const u8 {
slot
}
impl MsgVt {
#[must_use]
pub const fn new<F: MsgSlot>(table: &'static Table<F::Msg>) -> Self {
Self {
child: Child::Table(&table.raw),
place: place_impl::<F>,
get: get_impl::<F>,
direct: false,
}
}
#[must_use]
pub const fn new_via_message<F: MsgSlot>() -> Self
where
F::Msg: Message,
{
Self {
child: Child::of::<F::Msg>(),
place: place_impl::<F>,
get: get_impl::<F>,
direct: false,
}
}
#[must_use]
pub const fn direct<M>(table: &'static Table<M>) -> Self {
Self {
child: Child::Table(&table.raw),
place: direct_place,
get: direct_get,
direct: true,
}
}
#[must_use]
pub const fn direct_via_message<M: Message>() -> Self {
Self {
child: Child::of::<M>(),
place: direct_place,
get: direct_get,
direct: true,
}
}
}
pub struct DirectMsgVt<M> {
pub(super) vt: MsgVt,
_marker: PhantomData<fn(&M)>,
}
impl<M> DirectMsgVt<M> {
#[must_use]
pub const fn new(table: &'static Table<M>) -> Self {
Self {
vt: MsgVt::direct(table),
_marker: PhantomData,
}
}
#[must_use]
pub const fn via_message() -> Self
where
M: Message,
{
Self {
vt: MsgVt::direct_via_message::<M>(),
_marker: PhantomData,
}
}
}
pub struct RepVt {
pub(super) child: Child,
pub(super) size: usize,
pub(super) push: unsafe fn(*mut u8) -> *mut u8,
pub(super) pop: unsafe fn(*mut u8),
pub(super) parts: unsafe fn(*const u8) -> (*const u8, usize),
}
unsafe fn push_impl<T: Default>(slot: *mut u8) -> *mut u8 {
let v = unsafe { &mut *slot.cast::<Vec<T>>() };
v.push(T::default());
let last = v.len() - 1;
unsafe { v.as_mut_ptr().add(last).cast::<u8>() }
}
unsafe fn pop_impl<T>(slot: *mut u8) {
unsafe { (*slot.cast::<Vec<T>>()).pop() };
}
unsafe fn parts_impl<T>(slot: *const u8) -> (*const u8, usize) {
let v = unsafe { &*slot.cast::<Vec<T>>() };
(v.as_ptr().cast::<u8>(), v.len())
}
impl RepVt {
#[must_use]
pub const fn new<T: Default>(table: &'static Table<T>) -> Self {
Self {
child: Child::Table(&table.raw),
size: core::mem::size_of::<T>(),
push: push_impl::<T>,
pop: pop_impl::<T>,
parts: parts_impl::<T>,
}
}
#[must_use]
pub const fn new_via_message<T: Message>() -> Self {
Self {
child: Child::of::<T>(),
size: core::mem::size_of::<T>(),
push: push_impl::<T>,
pop: pop_impl::<T>,
parts: parts_impl::<T>,
}
}
}
pub struct EnumVt {
pub(super) card: u8,
pub(super) set: unsafe fn(*mut u8, i32) -> bool,
pub(super) get: unsafe fn(*const u8, usize) -> Option<i32>,
pub(super) len: unsafe fn(*const u8) -> usize,
pub(super) accepts: fn(i32) -> bool,
}
pub unsafe trait EnumShape {
type Slot;
const CARD: u8;
unsafe fn set(slot: *mut u8, raw: i32) -> bool;
unsafe fn get(slot: *const u8, idx: usize) -> Option<i32>;
unsafe fn len(_slot: *const u8) -> usize {
0
}
fn accepts(raw: i32) -> bool;
}
impl EnumVt {
#[must_use]
pub const fn new<S: EnumShape>() -> Self {
Self {
card: S::CARD,
set: S::set,
get: S::get,
len: S::len,
accepts: S::accepts,
}
}
}
pub(super) struct EnumVtOf<S>(PhantomData<S>);
impl<S: EnumShape> EnumVtOf<S> {
pub(super) const VT: EnumVt = EnumVt::new::<S>();
}
macro_rules! enum_shape {
($(#[$m:meta])* $name:ident, $card:ident, $slot:ty, $set:expr, $get:expr, $len:expr, $accepts:expr) => {
$(#[$m])*
pub struct $name<E>(PhantomData<E>);
unsafe impl<E: Enumeration> EnumShape for $name<E> {
type Slot = $slot;
const CARD: u8 = $card;
#[inline]
unsafe fn set(slot: *mut u8, raw: i32) -> bool {
let s = unsafe { &mut *slot.cast::<$slot>() };
($set)(s, raw)
}
#[inline]
unsafe fn get(slot: *const u8, idx: usize) -> Option<i32> {
let s = unsafe { &*slot.cast::<$slot>() };
($get)(s, idx)
}
#[inline]
unsafe fn len(slot: *const u8) -> usize {
let s = unsafe { &*slot.cast::<$slot>() };
($len)(s)
}
#[inline]
fn accepts(raw: i32) -> bool {
($accepts)(raw)
}
}
};
}
enum_shape!(
ImplicitOpen,
IMPLICIT,
EnumValue<E>,
|s: &mut EnumValue<E>, raw| {
*s = EnumValue::from(raw);
true
},
|s: &EnumValue<E>, _| Some(s.to_i32()),
|_: &EnumValue<E>| 0,
|_: i32| true
);
enum_shape!(
ImplicitClosed,
IMPLICIT,
E,
|s: &mut E, raw| match E::from_i32(raw) {
Some(v) => {
*s = v;
true
}
None => false,
},
|s: &E, _| Some(s.to_i32()),
|_: &E| 0,
|raw| E::from_i32(raw).is_some()
);
enum_shape!(
OptionalOpen,
OPTIONAL,
Option<EnumValue<E>>,
|s: &mut Option<EnumValue<E>>, raw| {
*s = Some(EnumValue::from(raw));
true
},
|s: &Option<EnumValue<E>>, _| s.as_ref().map(EnumValue::to_i32),
|_: &Option<EnumValue<E>>| 0,
|_: i32| true
);
enum_shape!(
OptionalClosed,
OPTIONAL,
Option<E>,
|s: &mut Option<E>, raw| match E::from_i32(raw) {
Some(v) => {
*s = Some(v);
true
}
None => false,
},
|s: &Option<E>, _| s.as_ref().map(Enumeration::to_i32),
|_: &Option<E>| 0,
|raw| E::from_i32(raw).is_some()
);
enum_shape!(
RepeatedOpen,
REPEATED,
Vec<EnumValue<E>>,
|s: &mut Vec<EnumValue<E>>, raw| {
s.push(EnumValue::from(raw));
true
},
|s: &Vec<EnumValue<E>>, i| s.get(i).map(EnumValue::to_i32),
|s: &Vec<EnumValue<E>>| s.len(),
|_: i32| true
);
enum_shape!(
RepeatedClosed,
REPEATED,
Vec<E>,
|s: &mut Vec<E>, raw| match E::from_i32(raw) {
Some(v) => {
s.push(v);
true
}
None => false,
},
|s: &Vec<E>, i| s.get(i).map(Enumeration::to_i32),
|s: &Vec<E>| s.len(),
|raw| E::from_i32(raw).is_some()
);