use super::bridge::write_field_value;
use super::map::write_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::encode_sink::PreSized;
use crate::encoding::encode_varint;
use crate::{types, EncodeSink, SizeCache, UnknownFields};
pub(super) unsafe fn write_to<K: EncodeSink>(
table: &MessageTable,
base: *const u8,
cache: &mut SizeCache,
buf: &mut K,
) {
let mut through_cursor = |cursor: &mut PreSized<'_>| {
unsafe { write_pre_sized(table, base, &mut *cache, cursor) }
};
if !buf.__with_pre_sized(&mut through_cursor) {
unsafe { write_message(table, base, cache, buf) };
}
}
#[inline(never)]
unsafe fn write_pre_sized(
table: &MessageTable,
base: *const u8,
cache: &mut SizeCache,
buf: &mut PreSized<'_>,
) {
unsafe { write_message(table, base, cache, buf) }
}
pub(super) unsafe fn write_message<K: EncodeSink>(
table: &MessageTable,
base: *const u8,
cache: &mut SizeCache,
buf: &mut K,
) {
for e in table.entries {
let slot = unsafe { base.add(e.offset as usize) };
unsafe {
if e.kind == Kind::Map {
write_map(table, e, slot, cache, buf);
} else {
write_kind(table, e, slot, cache, buf);
}
}
}
if table.unknown != NO_UNKNOWN {
unsafe { (*base.add(table.unknown as usize).cast::<UnknownFields>()).write_to(buf) };
}
}
#[inline(always)]
pub(super) fn put_tag<K: EncodeSink>(e: &Entry, buf: &mut K) {
if e.tag_len == 1 {
buf.put_u8(e.tag as u8);
} else {
encode_varint(u64::from(e.tag), buf);
}
}
macro_rules! write_dispatch {
($fname:ident; $($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
#[inline]
pub(super) unsafe fn $fname<K: EncodeSink>(
table: &MessageTable,
e: &Entry,
slot: *const u8,
cache: &mut SizeCache,
buf: &mut K,
) {
unsafe {
match e.kind {
$(Kind::$name => write_dispatch!(@arm $fam $ty $card table e slot cache buf),)*
#[allow(unreachable_patterns)]
_ => unreachable!("`Table::new` checked the kinds of the entries"),
}
}
}
};
(@arm Scalar $ty:ident $card:ident $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
write_scalar::<$ty, $card, K>($e, $slot, $buf)
};
(@arm Str $ty:ident $card:ident $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
write_str::<$card, K>($e, $slot, $buf)
};
(@arm Bytes $ty:ident $card:ident $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
write_bytes::<$card, K>($e, $slot, $buf)
};
(@arm Enum $ty:ident $card:ident $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
write_enum::<$card, K>($table, $e, $slot, $buf)
};
(@arm Msg $ty:ident $card:ident $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
write_msg::<$card, K>($table, $e, $slot, $cache, $buf)
};
(@arm Oneof $ty:ident LEADER $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
write_oneof::<K>($table, $e, $slot, $cache, $buf)
};
(@arm Oneof $ty:ident ONEOF $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
()
};
(@arm Map $ty:ident $card:ident $table:ident $e:ident $slot:ident $cache:ident $buf:ident) => {
unreachable!("a map field is written by `write_message`")
};
}
kind_table!(write_dispatch, write_kind);
payload_kind_table!(write_dispatch, write_payload);
#[inline(never)]
unsafe fn write_oneof<K: EncodeSink>(
table: &MessageTable,
e: &Entry,
slot: *const u8,
cache: &mut SizeCache,
buf: &mut K,
) {
let m = table.member(e);
let (number, payload) = unsafe { (table.group(m).get)(slot) };
if number == 0 {
return;
}
let payload_entry = table.payload_entry(m.group, number);
unsafe { write_payload(table, &payload_entry, payload, cache, buf) };
}
#[inline]
unsafe fn write_scalar<S: Sc, const C: u8, K: EncodeSink>(e: &Entry, slot: *const u8, buf: &mut K) {
unsafe {
match C {
IMPLICIT => {
let v = *slot.cast::<S::V>();
if !S::is_default(v) {
put_tag(e, buf);
S::encode(v, buf);
}
}
REQUIRED => {
put_tag(e, buf);
S::encode(*slot.cast::<S::V>(), buf);
}
OPTIONAL => {
if let Some(v) = *slot.cast::<Option<S::V>>() {
put_tag(e, buf);
S::encode(v, buf);
}
}
REPEATED => {
for &v in &*slot.cast::<Vec<S::V>>() {
put_tag(e, buf);
S::encode(v, buf);
}
}
_ => {
let v = &*slot.cast::<Vec<S::V>>();
if !v.is_empty() {
let payload: u64 = v.iter().map(|&x| S::len(x)).sum();
put_tag(e, buf);
encode_varint(payload, buf);
for &x in v {
S::encode(x, buf);
}
}
}
}
}
}
#[inline]
unsafe fn write_str<const C: u8, K: EncodeSink>(e: &Entry, slot: *const u8, buf: &mut K) {
unsafe {
match C {
IMPLICIT => {
let s = &*slot.cast::<String>();
if !s.is_empty() {
put_tag(e, buf);
types::encode_string(s, buf);
}
}
REQUIRED => {
put_tag(e, buf);
types::encode_string(&*slot.cast::<String>(), buf);
}
OPTIONAL => {
if let Some(s) = &*slot.cast::<Option<String>>() {
put_tag(e, buf);
types::encode_string(s, buf);
}
}
_ => {
for s in &*slot.cast::<Vec<String>>() {
put_tag(e, buf);
types::encode_string(s, buf);
}
}
}
}
}
#[inline]
unsafe fn write_bytes<const C: u8, K: EncodeSink>(e: &Entry, slot: *const u8, buf: &mut K) {
unsafe {
match C {
IMPLICIT => {
let s = &*slot.cast::<Vec<u8>>();
if !s.is_empty() {
put_tag(e, buf);
types::encode_shared_bytes(s, buf);
}
}
REQUIRED => {
put_tag(e, buf);
types::encode_shared_bytes(&*slot.cast::<Vec<u8>>(), buf);
}
OPTIONAL => {
if let Some(s) = &*slot.cast::<Option<Vec<u8>>>() {
put_tag(e, buf);
types::encode_shared_bytes(s, buf);
}
}
_ => {
for s in &*slot.cast::<Vec<Vec<u8>>>() {
put_tag(e, buf);
types::encode_shared_bytes(s, buf);
}
}
}
}
}
#[inline]
unsafe fn write_enum<const C: u8, K: EncodeSink>(
table: &MessageTable,
e: &Entry,
slot: *const u8,
buf: &mut K,
) {
let vt = table.enum_vt(e);
unsafe {
match C {
IMPLICIT => {
if let Some(v) = (vt.get)(slot, 0) {
if v != 0 {
put_tag(e, buf);
types::encode_int32(v, buf);
}
}
}
REQUIRED | OPTIONAL => {
if let Some(v) = (vt.get)(slot, 0) {
put_tag(e, buf);
types::encode_int32(v, buf);
}
}
REPEATED => {
for i in 0..(vt.len)(slot) {
if let Some(v) = (vt.get)(slot, i) {
put_tag(e, buf);
types::encode_int32(v, buf);
}
}
}
_ => {
let n = (vt.len)(slot);
if n == 0 {
return;
}
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;
}
}
put_tag(e, buf);
encode_varint(payload, buf);
for i in 0..n {
if let Some(v) = (vt.get)(slot, i) {
types::encode_int32(v, buf);
}
}
}
}
}
}
#[inline]
unsafe fn write_msg<const C: u8, K: EncodeSink>(
table: &MessageTable,
e: &Entry,
slot: *const u8,
cache: &mut SizeCache,
buf: &mut K,
) {
unsafe {
if C == REPEATED {
let vt = table.rep_vt(e);
let (ptr, len) = (vt.parts)(slot);
for i in 0..len {
put_tag(e, buf);
write_field_value(vt.child, ptr.add(i * vt.size), cache, buf);
}
} else {
let vt = table.msg_vt(e);
let child = (vt.get)(slot);
if !child.is_null() {
put_tag(e, buf);
write_field_value(vt.child, child, cache, buf);
}
}
}
}