Skip to content

Commit 0d7dfae

Browse files
committed
Move HandleStore into server.rs.
This fixes two `FIXME` comments, and makes things clearer, by not having server code in `client.rs`.
1 parent 3356d66 commit 0d7dfae

File tree

3 files changed

+104
-91
lines changed

3 files changed

+104
-91
lines changed

library/proc_macro/src/bridge/client.rs

+4-88
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ macro_rules! define_handles {
1111
) => {
1212
#[repr(C)]
1313
#[allow(non_snake_case)]
14-
pub struct HandleCounters {
15-
$($oty: AtomicUsize,)*
16-
$($ity: AtomicUsize,)*
14+
pub(super) struct HandleCounters {
15+
$(pub(super) $oty: AtomicUsize,)*
16+
$(pub(super) $ity: AtomicUsize,)*
1717
}
1818

1919
impl HandleCounters {
@@ -28,23 +28,6 @@ macro_rules! define_handles {
2828
}
2929
}
3030

31-
// FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
32-
#[repr(C)]
33-
#[allow(non_snake_case)]
34-
pub(super) struct HandleStore<S: server::Types> {
35-
$($oty: handle::OwnedStore<S::$oty>,)*
36-
$($ity: handle::InternedStore<S::$ity>,)*
37-
}
38-
39-
impl<S: server::Types> HandleStore<S> {
40-
pub(super) fn new(handle_counters: &'static HandleCounters) -> Self {
41-
HandleStore {
42-
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
43-
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
44-
}
45-
}
46-
}
47-
4831
$(
4932
#[repr(C)]
5033
pub(crate) struct $oty {
@@ -73,53 +56,18 @@ macro_rules! define_handles {
7356
}
7457
}
7558

76-
impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
77-
for Marked<S::$oty, $oty>
78-
{
79-
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
80-
s.$oty.take(handle::Handle::decode(r, &mut ()))
81-
}
82-
}
83-
8459
impl<S> Encode<S> for &$oty {
8560
fn encode(self, w: &mut Writer, s: &mut S) {
8661
self.handle.encode(w, s);
8762
}
8863
}
8964

90-
impl<'s, S: server::Types> Decode<'_, 's, HandleStore<server::MarkedTypes<S>>>
91-
for &'s Marked<S::$oty, $oty>
92-
{
93-
fn decode(r: &mut Reader<'_>, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self {
94-
&s.$oty[handle::Handle::decode(r, &mut ())]
95-
}
96-
}
97-
9865
impl<S> Encode<S> for &mut $oty {
9966
fn encode(self, w: &mut Writer, s: &mut S) {
10067
self.handle.encode(w, s);
10168
}
10269
}
10370

104-
impl<'s, S: server::Types> DecodeMut<'_, 's, HandleStore<server::MarkedTypes<S>>>
105-
for &'s mut Marked<S::$oty, $oty>
106-
{
107-
fn decode(
108-
r: &mut Reader<'_>,
109-
s: &'s mut HandleStore<server::MarkedTypes<S>>
110-
) -> Self {
111-
&mut s.$oty[handle::Handle::decode(r, &mut ())]
112-
}
113-
}
114-
115-
impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
116-
for Marked<S::$oty, $oty>
117-
{
118-
fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
119-
s.$oty.alloc(self).encode(w, s);
120-
}
121-
}
122-
12371
impl<S> DecodeMut<'_, '_, S> for $oty {
12472
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
12573
$oty {
@@ -147,22 +95,6 @@ macro_rules! define_handles {
14795
}
14896
}
14997

150-
impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
151-
for Marked<S::$ity, $ity>
152-
{
153-
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
154-
s.$ity.copy(handle::Handle::decode(r, &mut ()))
155-
}
156-
}
157-
158-
impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
159-
for Marked<S::$ity, $ity>
160-
{
161-
fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
162-
s.$ity.alloc(self).encode(w, s);
163-
}
164-
}
165-
16698
impl<S> DecodeMut<'_, '_, S> for $ity {
16799
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
168100
$ity {
@@ -174,23 +106,7 @@ macro_rules! define_handles {
174106
)*
175107
}
176108
}
177-
define_handles! {
178-
'owned:
179-
FreeFunctions,
180-
TokenStream,
181-
TokenStreamBuilder,
182-
TokenStreamIter,
183-
Group,
184-
Literal,
185-
SourceFile,
186-
MultiSpan,
187-
Diagnostic,
188-
189-
'interned:
190-
Punct,
191-
Ident,
192-
Span,
193-
}
109+
with_api_types!(define_handles);
194110

195111
// FIXME(eddyb) generate these impls by pattern-matching on the
196112
// names of methods - also could use the presence of `fn drop`

library/proc_macro/src/bridge/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,30 @@ macro_rules! with_api {
175175
};
176176
}
177177

178+
// Similar to `with_api`, but only lists the types, and they are divided into
179+
// the two storage categories.
180+
macro_rules! with_api_types {
181+
($m:ident) => {
182+
$m! {
183+
'owned:
184+
FreeFunctions,
185+
TokenStream,
186+
TokenStreamBuilder,
187+
TokenStreamIter,
188+
Group,
189+
Literal,
190+
SourceFile,
191+
MultiSpan,
192+
Diagnostic,
193+
194+
'interned:
195+
Punct,
196+
Ident,
197+
Span,
198+
}
199+
};
200+
}
201+
178202
// FIXME(eddyb) this calls `encode` for each argument, but in reverse,
179203
// to match the ordering in `reverse_decode`.
180204
macro_rules! reverse_encode {

library/proc_macro/src/bridge/server.rs

+76-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,81 @@
22
33
use super::*;
44

5-
// FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
6-
use super::client::HandleStore;
5+
macro_rules! define_handles {
6+
(
7+
'owned: $($oty:ident,)*
8+
'interned: $($ity:ident,)*
9+
) => {
10+
#[repr(C)]
11+
#[allow(non_snake_case)]
12+
struct HandleStore<S: Types> {
13+
$($oty: handle::OwnedStore<S::$oty>,)*
14+
$($ity: handle::InternedStore<S::$ity>,)*
15+
}
16+
17+
impl<S: Types> HandleStore<S> {
18+
fn new(handle_counters: &'static client::HandleCounters) -> Self {
19+
HandleStore {
20+
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
21+
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
22+
}
23+
}
24+
}
25+
26+
$(
27+
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>>
28+
for Marked<S::$oty, client::$oty>
29+
{
30+
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
31+
s.$oty.alloc(self).encode(w, s);
32+
}
33+
}
34+
35+
impl<S: Types> DecodeMut<'_, '_, HandleStore<MarkedTypes<S>>>
36+
for Marked<S::$oty, client::$oty>
37+
{
38+
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
39+
s.$oty.take(handle::Handle::decode(r, &mut ()))
40+
}
41+
}
42+
43+
impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
44+
for &'s Marked<S::$oty, client::$oty>
45+
{
46+
fn decode(r: &mut Reader<'_>, s: &'s HandleStore<MarkedTypes<S>>) -> Self {
47+
&s.$oty[handle::Handle::decode(r, &mut ())]
48+
}
49+
}
50+
51+
impl<'s, S: Types> DecodeMut<'_, 's, HandleStore<MarkedTypes<S>>>
52+
for &'s mut Marked<S::$oty, client::$oty>
53+
{
54+
fn decode(r: &mut Reader<'_>, s: &'s mut HandleStore<MarkedTypes<S>>) -> Self {
55+
&mut s.$oty[handle::Handle::decode(r, &mut ())]
56+
}
57+
}
58+
)*
59+
60+
$(
61+
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>>
62+
for Marked<S::$ity, client::$ity>
63+
{
64+
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
65+
s.$ity.alloc(self).encode(w, s);
66+
}
67+
}
68+
69+
impl<S: Types> DecodeMut<'_, '_, HandleStore<MarkedTypes<S>>>
70+
for Marked<S::$ity, client::$ity>
71+
{
72+
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
73+
s.$ity.copy(handle::Handle::decode(r, &mut ()))
74+
}
75+
}
76+
)*
77+
}
78+
}
79+
with_api_types!(define_handles);
780

881
pub trait Types {
982
type FreeFunctions: 'static;
@@ -46,7 +119,7 @@ macro_rules! declare_server_traits {
46119
}
47120
with_api!(Self, self_, declare_server_traits);
48121

49-
pub(super) struct MarkedTypes<S: Types>(S);
122+
struct MarkedTypes<S: Types>(S);
50123

51124
macro_rules! define_mark_types_impls {
52125
($($name:ident {

0 commit comments

Comments
 (0)