use core::marker::PhantomData;
use crate::alloc::{string::String, vec::Vec};
use crate::bytes::Buf;
use crate::encoding::{Tag, WireType};
use crate::{DecodeContext, DecodeError, EncodeSink, SizeCache, UnknownFields};
pub use map::MapVt;
pub use oneof::{Member, OneofEnum, OneofVt};
pub use shape::{
DirectMsgVt, EnumShape, EnumVt, ImplicitClosed, ImplicitOpen, MsgSlot, MsgVt, OptionalClosed,
OptionalOpen, RepVt, RepeatedClosed, RepeatedOpen,
};
use scalar::{
Bool, Double, Fixed32, Fixed64, Float, Int32, Int64, Sc, Sfixed32, Sfixed64, Sint32, Sint64,
Uint32, Uint64,
};
pub const ABI: u32 = 1;
const NO_UNKNOWN: u32 = u32::MAX;
#[doc(hidden)]
#[rustversion::since(1.77)]
pub use core::mem::offset_of;
#[doc(hidden)]
#[rustversion::before(1.77)]
#[macro_export]
macro_rules! __buffa_offset_of_unavailable {
($($tt:tt)*) => {
compile_error!(
"the table codec strategy requires Rust 1.77 or newer (`core::mem::offset_of!`)"
)
};
}
#[doc(hidden)]
#[rustversion::before(1.77)]
pub use __buffa_offset_of_unavailable as offset_of;
const IMPLICIT: u8 = 0;
const REQUIRED: u8 = 1;
const OPTIONAL: u8 = 2;
const REPEATED: u8 = 3;
const PACKED: u8 = 4;
macro_rules! kind_table {
($callback:ident $(, $fname:ident)?) => {
$callback! {
$($fname;)?
Int32Implicit: Scalar Int32 IMPLICIT;
Int32Required: Scalar Int32 REQUIRED;
Int32Optional: Scalar Int32 OPTIONAL;
Int32Repeated: Scalar Int32 REPEATED;
Int32Packed: Scalar Int32 PACKED;
Int64Implicit: Scalar Int64 IMPLICIT;
Int64Required: Scalar Int64 REQUIRED;
Int64Optional: Scalar Int64 OPTIONAL;
Int64Repeated: Scalar Int64 REPEATED;
Int64Packed: Scalar Int64 PACKED;
Uint32Implicit: Scalar Uint32 IMPLICIT;
Uint32Required: Scalar Uint32 REQUIRED;
Uint32Optional: Scalar Uint32 OPTIONAL;
Uint32Repeated: Scalar Uint32 REPEATED;
Uint32Packed: Scalar Uint32 PACKED;
Uint64Implicit: Scalar Uint64 IMPLICIT;
Uint64Required: Scalar Uint64 REQUIRED;
Uint64Optional: Scalar Uint64 OPTIONAL;
Uint64Repeated: Scalar Uint64 REPEATED;
Uint64Packed: Scalar Uint64 PACKED;
Sint32Implicit: Scalar Sint32 IMPLICIT;
Sint32Required: Scalar Sint32 REQUIRED;
Sint32Optional: Scalar Sint32 OPTIONAL;
Sint32Repeated: Scalar Sint32 REPEATED;
Sint32Packed: Scalar Sint32 PACKED;
Sint64Implicit: Scalar Sint64 IMPLICIT;
Sint64Required: Scalar Sint64 REQUIRED;
Sint64Optional: Scalar Sint64 OPTIONAL;
Sint64Repeated: Scalar Sint64 REPEATED;
Sint64Packed: Scalar Sint64 PACKED;
BoolImplicit: Scalar Bool IMPLICIT;
BoolRequired: Scalar Bool REQUIRED;
BoolOptional: Scalar Bool OPTIONAL;
BoolRepeated: Scalar Bool REPEATED;
BoolPacked: Scalar Bool PACKED;
Fixed32Implicit: Scalar Fixed32 IMPLICIT;
Fixed32Required: Scalar Fixed32 REQUIRED;
Fixed32Optional: Scalar Fixed32 OPTIONAL;
Fixed32Repeated: Scalar Fixed32 REPEATED;
Fixed32Packed: Scalar Fixed32 PACKED;
Fixed64Implicit: Scalar Fixed64 IMPLICIT;
Fixed64Required: Scalar Fixed64 REQUIRED;
Fixed64Optional: Scalar Fixed64 OPTIONAL;
Fixed64Repeated: Scalar Fixed64 REPEATED;
Fixed64Packed: Scalar Fixed64 PACKED;
Sfixed32Implicit: Scalar Sfixed32 IMPLICIT;
Sfixed32Required: Scalar Sfixed32 REQUIRED;
Sfixed32Optional: Scalar Sfixed32 OPTIONAL;
Sfixed32Repeated: Scalar Sfixed32 REPEATED;
Sfixed32Packed: Scalar Sfixed32 PACKED;
Sfixed64Implicit: Scalar Sfixed64 IMPLICIT;
Sfixed64Required: Scalar Sfixed64 REQUIRED;
Sfixed64Optional: Scalar Sfixed64 OPTIONAL;
Sfixed64Repeated: Scalar Sfixed64 REPEATED;
Sfixed64Packed: Scalar Sfixed64 PACKED;
FloatImplicit: Scalar Float IMPLICIT;
FloatRequired: Scalar Float REQUIRED;
FloatOptional: Scalar Float OPTIONAL;
FloatRepeated: Scalar Float REPEATED;
FloatPacked: Scalar Float PACKED;
DoubleImplicit: Scalar Double IMPLICIT;
DoubleRequired: Scalar Double REQUIRED;
DoubleOptional: Scalar Double OPTIONAL;
DoubleRepeated: Scalar Double REPEATED;
DoublePacked: Scalar Double PACKED;
StrImplicit: Str Str IMPLICIT;
StrRequired: Str Str REQUIRED;
StrOptional: Str Str OPTIONAL;
StrRepeated: Str Str REPEATED;
BytesImplicit: Bytes Bytes IMPLICIT;
BytesRequired: Bytes Bytes REQUIRED;
BytesOptional: Bytes Bytes OPTIONAL;
BytesRepeated: Bytes Bytes REPEATED;
EnumImplicit: Enum Enum IMPLICIT;
EnumRequired: Enum Enum REQUIRED;
EnumOptional: Enum Enum OPTIONAL;
EnumRepeated: Enum Enum REPEATED;
EnumPacked: Enum Enum PACKED;
MsgSingular: Msg Msg IMPLICIT;
MsgRepeated: Msg Msg REPEATED;
OneofLeader: Oneof Oneof LEADER;
OneofFollower: Oneof Oneof ONEOF;
Map: Map Map IMPLICIT;
}
};
}
macro_rules! payload_kind_table {
($callback:ident $(, $fname:ident)?) => {
$callback! {
$($fname;)?
Int32Required: Scalar Int32 REQUIRED;
Int64Required: Scalar Int64 REQUIRED;
Uint32Required: Scalar Uint32 REQUIRED;
Uint64Required: Scalar Uint64 REQUIRED;
Sint32Required: Scalar Sint32 REQUIRED;
Sint64Required: Scalar Sint64 REQUIRED;
BoolRequired: Scalar Bool REQUIRED;
Fixed32Required: Scalar Fixed32 REQUIRED;
Fixed64Required: Scalar Fixed64 REQUIRED;
Sfixed32Required: Scalar Sfixed32 REQUIRED;
Sfixed64Required: Scalar Sfixed64 REQUIRED;
FloatRequired: Scalar Float REQUIRED;
DoubleRequired: Scalar Double REQUIRED;
StrRequired: Str Str REQUIRED;
BytesRequired: Bytes Bytes REQUIRED;
EnumRequired: Enum Enum REQUIRED;
MsgSingular: Msg Msg IMPLICIT;
}
};
}
macro_rules! define_kind {
($($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u8)]
pub enum Kind {
$($name,)*
}
impl Kind {
const fn wire_type(self) -> u32 {
match self {
$(Kind::$name => define_kind!(@wire $fam $ty $card),)*
}
}
const fn aux_kind(self) -> Option<AuxKind> {
match self {
$(Kind::$name => define_kind!(@aux $fam $card),)*
}
}
const fn card(self) -> u8 {
match self {
$(Kind::$name => define_kind!(@card $card),)*
}
}
const fn shape_card(self) -> u8 {
match self {
$(Kind::$name => define_kind!(@shape $card),)*
}
}
}
pub mod kinds {
#[allow(clippy::wildcard_imports)]
use super::*;
$(
#[doc = concat!("The type-level name of [`Kind::", stringify!($name), "`](super::Kind::", stringify!($name), ").")]
pub struct $name;
impl KindMarker for $name { const KIND: Kind = Kind::$name; }
define_kind!(@slot $name $fam $ty $card);
)*
}
};
(@slot $name:ident Scalar $ty:ident IMPLICIT) => { impl KindSlot for $name { type Slot = <$ty as Sc>::V; } };
(@slot $name:ident Scalar $ty:ident REQUIRED) => { impl KindSlot for $name { type Slot = <$ty as Sc>::V; } };
(@slot $name:ident Scalar $ty:ident OPTIONAL) => { impl KindSlot for $name { type Slot = Option<<$ty as Sc>::V>; } };
(@slot $name:ident Scalar $ty:ident REPEATED) => { impl KindSlot for $name { type Slot = Vec<<$ty as Sc>::V>; } };
(@slot $name:ident Scalar $ty:ident PACKED) => { impl KindSlot for $name { type Slot = Vec<<$ty as Sc>::V>; } };
(@slot $name:ident Str $ty:ident IMPLICIT) => { impl KindSlot for $name { type Slot = String; } };
(@slot $name:ident Str $ty:ident REQUIRED) => { impl KindSlot for $name { type Slot = String; } };
(@slot $name:ident Str $ty:ident OPTIONAL) => { impl KindSlot for $name { type Slot = Option<String>; } };
(@slot $name:ident Str $ty:ident REPEATED) => { impl KindSlot for $name { type Slot = Vec<String>; } };
(@slot $name:ident Bytes $ty:ident IMPLICIT) => { impl KindSlot for $name { type Slot = Vec<u8>; } };
(@slot $name:ident Bytes $ty:ident REQUIRED) => { impl KindSlot for $name { type Slot = Vec<u8>; } };
(@slot $name:ident Bytes $ty:ident OPTIONAL) => { impl KindSlot for $name { type Slot = Option<Vec<u8>>; } };
(@slot $name:ident Bytes $ty:ident REPEATED) => { impl KindSlot for $name { type Slot = Vec<Vec<u8>>; } };
(@slot $name:ident Enum $ty:ident $card:ident) => {};
(@slot $name:ident Msg $ty:ident $card:ident) => {};
(@slot $name:ident Oneof $ty:ident $card:ident) => {};
(@slot $name:ident Map $ty:ident $card:ident) => {};
(@wire Scalar $ty:ident PACKED) => { WireType::LengthDelimited as u32 };
(@wire Scalar $ty:ident $card:ident) => { <$ty as Sc>::WIRE as u32 };
(@wire Str $ty:ident $card:ident) => { WireType::LengthDelimited as u32 };
(@wire Bytes $ty:ident $card:ident) => { WireType::LengthDelimited as u32 };
(@wire Msg $ty:ident $card:ident) => { WireType::LengthDelimited as u32 };
(@wire Map $ty:ident $card:ident) => { WireType::LengthDelimited as u32 };
(@wire Enum $ty:ident PACKED) => { WireType::LengthDelimited as u32 };
(@wire Enum $ty:ident $card:ident) => { WireType::Varint as u32 };
(@wire Oneof $ty:ident $card:ident) => {
panic!("a oneof member's wire type is its payload kind's, so build its entry with `Entry::oneof_member`")
};
(@card ONEOF) => { IMPLICIT };
(@card LEADER) => { IMPLICIT };
(@card $card:ident) => { $card };
(@shape IMPLICIT) => { IMPLICIT };
(@shape REQUIRED) => { IMPLICIT };
(@shape OPTIONAL) => { OPTIONAL };
(@shape REPEATED) => { REPEATED };
(@shape PACKED) => { REPEATED };
(@shape ONEOF) => { IMPLICIT };
(@shape LEADER) => { IMPLICIT };
(@aux Scalar $card:ident) => { None };
(@aux Str $card:ident) => { None };
(@aux Bytes $card:ident) => { None };
(@aux Enum $card:ident) => { Some(AuxKind::Enum) };
(@aux Oneof $card:ident) => { Some(AuxKind::Member) };
(@aux Map $card:ident) => { Some(AuxKind::Map) };
(@aux Msg REPEATED) => { Some(AuxKind::Rep) };
(@aux Msg $card:ident) => { Some(AuxKind::Msg) };
}
pub trait KindSlot: KindMarker {
type Slot;
}
pub trait KindMarker {
const KIND: Kind;
}
kind_table!(define_kind);
macro_rules! define_payload_check {
($($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
impl Kind {
const fn is_oneof_payload(self) -> bool {
matches!(self, $(Kind::$name)|*)
}
}
};
}
payload_kind_table!(define_payload_check);
mod bridge;
mod decode;
mod encode;
mod map;
mod oneof;
mod scalar;
mod shape;
mod size;
#[derive(Clone, Copy, PartialEq, Eq)]
enum AuxKind {
Msg,
Rep,
Enum,
Group,
Member,
Map,
}
pub enum Aux {
Msg(&'static MsgVt),
Rep(&'static RepVt),
Enum(&'static EnumVt),
Group(&'static OneofVt),
Member(Member),
Map(&'static MapVt),
}
impl Aux {
const fn kind(&self) -> AuxKind {
match self {
Aux::Msg(_) => AuxKind::Msg,
Aux::Rep(_) => AuxKind::Rep,
Aux::Enum(_) => AuxKind::Enum,
Aux::Group(_) => AuxKind::Group,
Aux::Member(_) => AuxKind::Member,
Aux::Map(_) => AuxKind::Map,
}
}
}
const _: () = assert!(core::mem::size_of::<Entry>() == 12);
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Entry {
tag: u32,
offset: u32,
kind: Kind,
tag_len: u8,
aux: u16,
}
impl Entry {
#[must_use]
pub const fn new(kind: Kind, number: u32, offset: usize, aux: u16) -> Self {
assert!(
number >= 1 && number < (1 << 29),
"field number out of range"
);
assert!(offset <= u32::MAX as usize, "field offset out of range");
let tag = (number << 3) | kind.wire_type();
let mut tag_len = 1u8;
let mut rest = tag >> 7;
while rest != 0 {
tag_len += 1;
rest >>= 7;
}
Self {
tag,
offset: offset as u32,
kind,
tag_len,
aux,
}
}
#[must_use]
pub const fn oneof_member(
payload: Kind,
leader: bool,
number: u32,
offset: usize,
aux: u16,
) -> Self {
assert!(
payload.is_oneof_payload(),
"a oneof member's payload must be a `Required` scalar, string, bytes or enum kind, or `MsgSingular`"
);
let mut e = Self::new(payload, number, offset, aux);
e.kind = if leader {
Kind::OneofLeader
} else {
Kind::OneofFollower
};
e
}
const fn number(&self) -> u32 {
self.tag >> 3
}
}
pub struct MessageTable {
entries: &'static [Entry],
dense: &'static [u8],
aux: &'static [Aux],
unknown: u32,
}
impl MessageTable {
#[inline]
fn find(&self, number: u32) -> Option<&Entry> {
if let Some(&i) = self.dense.get(number as usize) {
return match i {
0 => None,
i => self.entries.get(usize::from(i) - 1),
};
}
self.entries
.binary_search_by_key(&number, Entry::number)
.ok()
.map(|i| &self.entries[i])
}
#[inline]
fn msg_vt(&self, e: &Entry) -> &'static MsgVt {
match &self.aux[usize::from(e.aux)] {
Aux::Msg(vt) => vt,
_ => unreachable!("`Table::new` checked that message entries index `MsgVt`s"),
}
}
#[inline]
fn rep_vt(&self, e: &Entry) -> &'static RepVt {
match &self.aux[usize::from(e.aux)] {
Aux::Rep(vt) => vt,
_ => unreachable!("`Table::new` checked that repeated message entries index `RepVt`s"),
}
}
#[inline]
fn map_vt(&self, e: &Entry) -> &'static MapVt {
match &self.aux[usize::from(e.aux)] {
Aux::Map(vt) => vt,
_ => unreachable!("`Table::new` checked that map entries index `MapVt`s"),
}
}
#[inline]
fn enum_vt(&self, e: &Entry) -> &'static EnumVt {
match &self.aux[usize::from(e.aux)] {
Aux::Enum(vt) => vt,
_ => unreachable!("`Table::new` checked that enum entries index enum descriptors"),
}
}
#[inline]
fn member(&self, e: &Entry) -> Member {
match &self.aux[usize::from(e.aux)] {
Aux::Member(m) => *m,
_ => unreachable!("`Table::new` checked that oneof entries index `Member`s"),
}
}
#[inline]
fn group(&self, m: Member) -> &'static OneofVt {
match &self.aux[usize::from(m.group)] {
Aux::Group(g) => g,
_ => unreachable!("`Table::new` checked that members index oneof descriptors"),
}
}
#[inline]
fn payload_entry(&self, group: u16, number: u32) -> Entry {
let Some(e) = self.find(number) else {
oneof::no_such_member(number)
};
if !matches!(e.kind, Kind::OneofLeader | Kind::OneofFollower) {
oneof::no_such_member(number)
}
let m = self.member(e);
if m.group != group {
oneof::no_such_member(number)
}
Entry {
kind: m.kind,
aux: m.aux,
..*e
}
}
}
pub struct Table<M> {
raw: MessageTable,
_marker: PhantomData<fn(&M)>,
}
impl<M> Table<M> {
#[must_use]
pub const unsafe fn new(
abi: u32,
entries: &'static [Entry],
dense: &'static [u8],
aux: &'static [Aux],
unknown: Option<usize>,
) -> Self {
assert!(
abi == ABI,
"buffa table: the generated code is for a different table ABI; \
regenerate it with the buffa-codegen that matches this buffa"
);
let size = core::mem::size_of::<M>();
let mut leaders = 0;
let mut i = 0;
while i < entries.len() {
let e = &entries[i];
assert!(
i == 0 || entries[i - 1].number() < e.number(),
"buffa table: entries are not in strictly increasing field-number order"
);
assert!(
(e.offset as usize) < size,
"buffa table: an entry's offset is outside the message struct"
);
if let Some(want) = e.kind.aux_kind() {
assert!(
(e.aux as usize) < aux.len(),
"buffa table: an entry's aux index is out of range"
);
let a = &aux[e.aux as usize];
assert!(
a.kind() as u8 == want as u8,
"buffa table: an entry's aux descriptor is the wrong variant for its kind"
);
if let Aux::Enum(vt) = a {
assert!(
vt.card == e.kind.shape_card(),
"buffa table: an enum entry's descriptor has the wrong cardinality for its kind"
);
}
if let Aux::Msg(vt) = a {
assert!(
!vt.direct,
"buffa table: a message field's descriptor must not be a `MsgVt::direct` one"
);
}
if let Aux::Member(m) = a {
if oneof::check_member(e, *m, aux) {
leaders += 1;
}
}
}
i += 1;
}
let mut groups = 0;
i = 0;
while i < aux.len() {
if let Aux::Group(_) = &aux[i] {
groups += 1;
}
i += 1;
}
assert!(
leaders == groups,
"buffa table: every oneof descriptor needs exactly one leading member"
);
let mut named = 0;
let mut n = 0;
while n < dense.len() {
let d = dense[n] as usize;
if d != 0 {
assert!(
d <= entries.len() && entries[d - 1].number() as usize == n,
"buffa table: the dense lookup names an entry with the wrong field number"
);
named += 1;
}
n += 1;
}
let mut covered = 0;
i = 0;
while i < entries.len() {
if (entries[i].number() as usize) < dense.len() {
covered += 1;
}
i += 1;
}
assert!(
named == covered,
"buffa table: the dense lookup omits an entry"
);
let unknown = match unknown {
None => NO_UNKNOWN,
Some(offset) => {
assert!(
offset < NO_UNKNOWN as usize
&& offset <= size
&& size - offset >= core::mem::size_of::<UnknownFields>()
&& offset % core::mem::align_of::<UnknownFields>() == 0,
"buffa table: the unknown-fields offset does not fit an `UnknownFields` in the message struct"
);
offset as u32
}
};
Self {
raw: MessageTable {
entries,
dense,
aux,
unknown,
},
_marker: PhantomData,
}
}
#[inline]
pub fn compute_size(&self, msg: &M, cache: &mut SizeCache) -> u32 {
unsafe { size::compute_size(&self.raw, (msg as *const M).cast(), cache) }
}
#[inline]
pub fn write_to<K: EncodeSink>(&self, msg: &M, cache: &mut SizeCache, buf: &mut K) {
let base = (msg as *const M).cast::<u8>();
unsafe { encode::write_to(&self.raw, base, cache, buf) }
}
#[inline]
pub fn merge_to_limit<B: Buf>(
&self,
msg: &mut M,
buf: &mut B,
ctx: DecodeContext<'_>,
limit: usize,
) -> Result<(), DecodeError> {
let base = (msg as *mut M).cast::<u8>();
unsafe { decode::merge_to_limit(&self.raw, base, buf, ctx, limit) }
}
#[inline]
pub fn merge_length_delimited<B: Buf>(
&self,
msg: &mut M,
buf: &mut B,
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let base = (msg as *mut M).cast::<u8>();
unsafe { decode::merge_length_delimited(&self.raw, base, buf, ctx) }
}
#[inline]
pub fn merge_field<B: Buf>(
&self,
msg: &mut M,
tag: Tag,
buf: &mut B,
ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
let base = (msg as *mut M).cast::<u8>();
unsafe { decode::merge_field(&self.raw, base, tag, buf, ctx) }
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! __table {
(
$msg:ty,
abi = $abi:expr,
entries = [$($entry:expr),* $(,)?],
dense = $dense:expr,
aux = [$($aux:expr),* $(,)?],
unknown = none $(,)?
) => {
unsafe {
$crate::table::Table::<$msg>::new(
$abi,
&[$($entry),*],
$dense,
&[$($aux),*],
::core::option::Option::None,
)
}
};
(
$msg:ty,
abi = $abi:expr,
entries = [$($entry:expr),* $(,)?],
dense = $dense:expr,
aux = [$($aux:expr),* $(,)?],
unknown = $unknown:ident $(,)?
) => {
{
const _: fn(&$msg) -> *const $crate::UnknownFields =
|m| ::core::ptr::addr_of!(m.$unknown);
unsafe {
$crate::table::Table::<$msg>::new(
$abi,
&[$($entry),*],
$dense,
&[$($aux),*],
::core::option::Option::Some($crate::table::offset_of!($msg, $unknown)),
)
}
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __table_entry {
(
$msg:ty, $field:ident, oneof($payload:ident, $leader:expr), $number:expr,
aux = $aux:expr, slot = $slot:ty $(,)?
) => {{
const _: fn(&$msg) -> *const $slot = |m| ::core::ptr::addr_of!(m.$field);
$crate::table::Entry::oneof_member(
$crate::table::Kind::$payload,
$leader,
$number,
$crate::table::offset_of!($msg, $field),
$aux,
)
}};
($msg:ty, $field:ident, $kind:ident, $number:expr $(,)?) => {{
const _: fn(
&$msg,
)
-> *const <$crate::table::kinds::$kind as $crate::table::KindSlot>::Slot =
|m| ::core::ptr::addr_of!(m.$field);
$crate::table::Entry::new(
$crate::table::Kind::$kind,
$number,
$crate::table::offset_of!($msg, $field),
0,
)
}};
($msg:ty, $field:ident, $kind:ident, $number:expr, aux = $aux:expr, slot = $slot:ty $(,)?) => {{
const _: fn(&$msg) -> *const $slot = |m| ::core::ptr::addr_of!(m.$field);
$crate::table::Entry::new(
$crate::table::Kind::$kind,
$number,
$crate::table::offset_of!($msg, $field),
$aux,
)
}};
}
#[cfg(test)]
#[rustversion::since(1.77)]
mod tests {
include!("tests.rs");
}
#[cfg(test)]
#[rustversion::since(1.77)]
mod map_tests {
include!("map_tests.rs");
}