use super::map::size_map;
use super::scalar::Sc;
use super::{
Bool, Double, Entry, Fixed32, Fixed64, Float, Int32, Int64, Kind, MessageTable, Sfixed32,
Sfixed64, Sint32, Sint64, Uint32, Uint64, IMPLICIT, NO_UNKNOWN, OPTIONAL, PACKED, REPEATED,
REQUIRED,
};
use crate::alloc::{string::String, vec::Vec};
use crate::encoding::varint_len;
use crate::{types, SizeCache, UnknownFields};
pub(super) unsafe fn compute_size(
table: &MessageTable,
base: *const u8,
cache: &mut SizeCache,
) -> u32 {
let mut size = 0u64;
for e in table.entries {
let slot = unsafe { base.add(e.offset as usize) };
size += unsafe {
if e.kind == Kind::Map {
size_map(table, e, slot, cache)
} else {
size_kind(table, e, slot, cache)
}
};
}
if table.unknown != NO_UNKNOWN {
size += unsafe { (*base.add(table.unknown as usize).cast::<UnknownFields>()).encoded_len() }
as u64;
}
crate::saturate_size(size)
}
macro_rules! size_dispatch {
($fname:ident; $($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
#[inline]
pub(super) unsafe fn $fname(
table: &MessageTable,
e: &Entry,
slot: *const u8,
cache: &mut SizeCache,
) -> u64 {
let tl = u64::from(e.tag_len);
unsafe {
match e.kind {
$(Kind::$name => size_dispatch!(@arm $fam $ty $card table e tl slot cache),)*
#[allow(unreachable_patterns)]
_ => unreachable!("`Table::new` checked the kinds of the entries"),
}
}
}
};
(@arm Scalar $ty:ident $card:ident $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
size_scalar::<$ty, $card>($tl, $slot)
};
(@arm Str $ty:ident $card:ident $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
size_str::<$card>($tl, $slot)
};
(@arm Bytes $ty:ident $card:ident $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
size_bytes::<$card>($tl, $slot)
};
(@arm Enum $ty:ident $card:ident $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
size_enum::<$card>($table, $e, $tl, $slot)
};
(@arm Msg $ty:ident $card:ident $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
size_msg::<$card>($table, $e, $tl, $slot, $cache)
};
(@arm Oneof $ty:ident LEADER $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
size_oneof($table, $e, $slot, $cache)
};
(@arm Oneof $ty:ident ONEOF $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
0
};
(@arm Map $ty:ident $card:ident $table:ident $e:ident $tl:ident $slot:ident $cache:ident) => {
unreachable!("a map field is sized by `compute_size`")
};
}
kind_table!(size_dispatch, size_kind);
payload_kind_table!(size_dispatch, size_payload);
#[inline(never)]
unsafe fn size_oneof(
table: &MessageTable,
e: &Entry,
slot: *const u8,
cache: &mut SizeCache,
) -> u64 {
let m = table.member(e);
let (number, payload) = unsafe { (table.group(m).get)(slot) };
if number == 0 {
return 0;
}
let payload_entry = table.payload_entry(m.group, number);
unsafe { size_payload(table, &payload_entry, payload, cache) }
}
#[inline]
unsafe fn size_scalar<S: Sc, const C: u8>(tl: u64, slot: *const u8) -> u64 {
unsafe {
match C {
IMPLICIT => {
let v = *slot.cast::<S::V>();
if S::is_default(v) {
0
} else {
tl + S::len(v)
}
}
REQUIRED => tl + S::len(*slot.cast::<S::V>()),
OPTIONAL => match *slot.cast::<Option<S::V>>() {
Some(v) => tl + S::len(v),
None => 0,
},
REPEATED => (*slot.cast::<Vec<S::V>>())
.iter()
.map(|&v| tl + S::len(v))
.sum(),
_ => {
let v = &*slot.cast::<Vec<S::V>>();
if v.is_empty() {
0
} else {
let payload: u64 = v.iter().map(|&x| S::len(x)).sum();
tl + varint_len(payload) as u64 + payload
}
}
}
}
}
#[inline]
unsafe fn size_str<const C: u8>(tl: u64, slot: *const u8) -> u64 {
unsafe {
match C {
IMPLICIT => {
let s = &*slot.cast::<String>();
if s.is_empty() {
0
} else {
tl + types::string_encoded_len(s) as u64
}
}
REQUIRED => tl + types::string_encoded_len(&*slot.cast::<String>()) as u64,
OPTIONAL => match &*slot.cast::<Option<String>>() {
Some(s) => tl + types::string_encoded_len(s) as u64,
None => 0,
},
_ => (*slot.cast::<Vec<String>>())
.iter()
.map(|s| tl + types::string_encoded_len(s) as u64)
.sum(),
}
}
}
#[inline]
unsafe fn size_bytes<const C: u8>(tl: u64, slot: *const u8) -> u64 {
unsafe {
match C {
IMPLICIT => {
let s = &*slot.cast::<Vec<u8>>();
if s.is_empty() {
0
} else {
tl + types::bytes_encoded_len(s) as u64
}
}
REQUIRED => tl + types::bytes_encoded_len(&*slot.cast::<Vec<u8>>()) as u64,
OPTIONAL => match &*slot.cast::<Option<Vec<u8>>>() {
Some(s) => tl + types::bytes_encoded_len(s) as u64,
None => 0,
},
_ => (*slot.cast::<Vec<Vec<u8>>>())
.iter()
.map(|s| tl + types::bytes_encoded_len(s) as u64)
.sum(),
}
}
}
#[inline]
unsafe fn size_enum<const C: u8>(table: &MessageTable, e: &Entry, tl: u64, slot: *const u8) -> u64 {
let vt = table.enum_vt(e);
unsafe {
match C {
IMPLICIT => match (vt.get)(slot, 0) {
Some(v) if v != 0 => tl + types::int32_encoded_len(v) as u64,
_ => 0,
},
REQUIRED | OPTIONAL => match (vt.get)(slot, 0) {
Some(v) => tl + types::int32_encoded_len(v) as u64,
None => 0,
},
REPEATED => {
let mut size = 0;
for i in 0..(vt.len)(slot) {
if let Some(v) = (vt.get)(slot, i) {
size += tl + types::int32_encoded_len(v) as u64;
}
}
size
}
_ => {
let n = (vt.len)(slot);
if n == 0 {
return 0;
}
let mut payload = 0;
for i in 0..n {
if let Some(v) = (vt.get)(slot, i) {
payload += types::int32_encoded_len(v) as u64;
}
}
tl + varint_len(payload) as u64 + payload
}
}
}
}
#[inline]
unsafe fn size_msg<const C: u8>(
table: &MessageTable,
e: &Entry,
tl: u64,
slot: *const u8,
cache: &mut SizeCache,
) -> u64 {
unsafe {
if C == REPEATED {
let vt = table.rep_vt(e);
let (ptr, len) = (vt.parts)(slot);
let mut size = 0;
for i in 0..len {
let idx = cache.reserve();
let inner = vt.child.compute_size(ptr.add(i * vt.size), cache);
cache.set(idx, inner);
size += tl + varint_len(u64::from(inner)) as u64 + u64::from(inner);
}
size
} else {
let vt = table.msg_vt(e);
let child = (vt.get)(slot);
if child.is_null() {
return 0;
}
let idx = cache.reserve();
let inner = vt.child.compute_size(child, cache);
cache.set(idx, inner);
tl + varint_len(u64::from(inner)) as u64 + u64::from(inner)
}
}
}