Expand with_api_handle_types

This commit is contained in:
bjorn3 2025-10-03 21:29:58 +00:00
parent dabae7eea4
commit 4dc28c59ab
3 changed files with 98 additions and 150 deletions

View file

@ -6,87 +6,66 @@ use std::sync::atomic::AtomicU32;
use super::*;
macro_rules! define_client_handles {
(
'owned: $($oty:ident,)*
'interned: $($ity:ident,)*
) => {
#[repr(C)]
#[allow(non_snake_case)]
pub(super) struct HandleCounters {
$(pub(super) $oty: AtomicU32,)*
$(pub(super) $ity: AtomicU32,)*
}
#[repr(C)]
pub(super) struct HandleCounters {
pub(super) token_stream: AtomicU32,
pub(super) span: AtomicU32,
}
static COUNTERS: HandleCounters = HandleCounters {
$($oty: AtomicU32::new(1),)*
$($ity: AtomicU32::new(1),)*
};
static COUNTERS: HandleCounters =
HandleCounters { token_stream: AtomicU32::new(1), span: AtomicU32::new(1) };
$(
pub(crate) struct $oty {
handle: handle::Handle,
}
pub(crate) struct TokenStream {
handle: handle::Handle,
}
impl !Send for $oty {}
impl !Sync for $oty {}
impl !Send for TokenStream {}
impl !Sync for TokenStream {}
// Forward `Drop::drop` to the inherent `drop` method.
impl Drop for $oty {
fn drop(&mut self) {
$oty {
handle: self.handle,
}.drop();
}
}
impl<S> Encode<S> for $oty {
fn encode(self, w: &mut Writer, s: &mut S) {
mem::ManuallyDrop::new(self).handle.encode(w, s);
}
}
impl<S> Encode<S> for &$oty {
fn encode(self, w: &mut Writer, s: &mut S) {
self.handle.encode(w, s);
}
}
impl<S> Decode<'_, '_, S> for $oty {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$oty {
handle: handle::Handle::decode(r, s),
}
}
}
)*
$(
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct $ity {
handle: handle::Handle,
}
impl !Send for $ity {}
impl !Sync for $ity {}
impl<S> Encode<S> for $ity {
fn encode(self, w: &mut Writer, s: &mut S) {
self.handle.encode(w, s);
}
}
impl<S> Decode<'_, '_, S> for $ity {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$ity {
handle: handle::Handle::decode(r, s),
}
}
}
)*
// Forward `Drop::drop` to the inherent `drop` method.
impl Drop for TokenStream {
fn drop(&mut self) {
TokenStream { handle: self.handle }.drop();
}
}
impl<S> Encode<S> for TokenStream {
fn encode(self, w: &mut Writer, s: &mut S) {
mem::ManuallyDrop::new(self).handle.encode(w, s);
}
}
impl<S> Encode<S> for &TokenStream {
fn encode(self, w: &mut Writer, s: &mut S) {
self.handle.encode(w, s);
}
}
impl<S> Decode<'_, '_, S> for TokenStream {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
TokenStream { handle: handle::Handle::decode(r, s) }
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct Span {
handle: handle::Handle,
}
impl !Send for Span {}
impl !Sync for Span {}
impl<S> Encode<S> for Span {
fn encode(self, w: &mut Writer, s: &mut S) {
self.handle.encode(w, s);
}
}
impl<S> Decode<'_, '_, S> for Span {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
Span { handle: handle::Handle::decode(r, s) }
}
}
with_api_handle_types!(define_client_handles);
// FIXME(eddyb) generate these impls by pattern-matching on the
// names of methods - also could use the presence of `fn drop`

View file

@ -102,22 +102,6 @@ macro_rules! with_api {
};
}
// Similar to `with_api`, but only lists the types requiring handles, and they
// are divided into the two storage categories.
macro_rules! with_api_handle_types {
($m:ident) => {
$m! {
'owned:
// FreeFunctions is handled manually
TokenStream,
'interned:
Span,
// Symbol is handled manually
}
};
}
pub(crate) struct FreeFunctions;
#[allow(unsafe_code)]

View file

@ -5,68 +5,53 @@ use std::marker::PhantomData;
use super::*;
macro_rules! define_server_handles {
(
'owned: $($oty:ident,)*
'interned: $($ity:ident,)*
) => {
#[allow(non_snake_case)]
pub(super) struct HandleStore<S: Types> {
$($oty: handle::OwnedStore<S::$oty>,)*
$($ity: handle::InternedStore<S::$ity>,)*
pub(super) struct HandleStore<S: Types> {
token_stream: handle::OwnedStore<S::TokenStream>,
span: handle::InternedStore<S::Span>,
}
impl<S: Types> HandleStore<S> {
fn new(handle_counters: &'static client::HandleCounters) -> Self {
HandleStore {
token_stream: handle::OwnedStore::new(&handle_counters.token_stream),
span: handle::InternedStore::new(&handle_counters.span),
}
impl<S: Types> HandleStore<S> {
fn new(handle_counters: &'static client::HandleCounters) -> Self {
HandleStore {
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
}
}
}
$(
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>> for Marked<S::$oty, client::$oty> {
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
s.$oty.alloc(self).encode(w, s);
}
}
impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>>
for Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.$oty.take(handle::Handle::decode(r, &mut ()))
}
}
impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
for &'s Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &'s mut HandleStore<MarkedTypes<S>>) -> Self {
&s.$oty[handle::Handle::decode(r, &mut ())]
}
}
)*
$(
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>> for Marked<S::$ity, client::$ity> {
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
s.$ity.alloc(self).encode(w, s);
}
}
impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>>
for Marked<S::$ity, client::$ity>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.$ity.copy(handle::Handle::decode(r, &mut ()))
}
}
)*
}
}
with_api_handle_types!(define_server_handles);
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>> for Marked<S::TokenStream, client::TokenStream> {
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
s.token_stream.alloc(self).encode(w, s);
}
}
impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>>
for Marked<S::TokenStream, client::TokenStream>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.token_stream.take(handle::Handle::decode(r, &mut ()))
}
}
impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
for &'s Marked<S::TokenStream, client::TokenStream>
{
fn decode(r: &mut Reader<'_>, s: &'s mut HandleStore<MarkedTypes<S>>) -> Self {
&s.token_stream[handle::Handle::decode(r, &mut ())]
}
}
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>> for Marked<S::Span, client::Span> {
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
s.span.alloc(self).encode(w, s);
}
}
impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>> for Marked<S::Span, client::Span> {
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.span.copy(handle::Handle::decode(r, &mut ()))
}
}
pub trait Types {
type FreeFunctions: 'static;