Skip to content

Commit 0f4f8e5

Browse files
committed
proc_macro: support encoding/decoding Vec<T>
1 parent 674525c commit 0f4f8e5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

library/proc_macro/src/bridge/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,21 @@ impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
329329
}
330330
}
331331

332+
impl<T: Mark> Mark for Vec<T> {
333+
type Unmarked = Vec<T::Unmarked>;
334+
fn mark(unmarked: Self::Unmarked) -> Self {
335+
// Should be a no-op due to std's in-place collect optimizations.
336+
unmarked.into_iter().map(T::mark).collect()
337+
}
338+
}
339+
impl<T: Unmark> Unmark for Vec<T> {
340+
type Unmarked = Vec<T::Unmarked>;
341+
fn unmark(self) -> Self::Unmarked {
342+
// Should be a no-op due to std's in-place collect optimizations.
343+
self.into_iter().map(T::unmark).collect()
344+
}
345+
}
346+
332347
macro_rules! mark_noop {
333348
($($ty:ty),* $(,)?) => {
334349
$(

library/proc_macro/src/bridge/rpc.rs

+20
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,26 @@ impl<S> DecodeMut<'_, '_, S> for String {
248248
}
249249
}
250250

251+
impl<S, T: Encode<S>> Encode<S> for Vec<T> {
252+
fn encode(self, w: &mut Writer, s: &mut S) {
253+
self.len().encode(w, s);
254+
for x in self {
255+
x.encode(w, s);
256+
}
257+
}
258+
}
259+
260+
impl<S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T> {
261+
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
262+
let len = usize::decode(r, s);
263+
let mut vec = Vec::with_capacity(len);
264+
for _ in 0..len {
265+
vec.push(T::decode(r, s));
266+
}
267+
vec
268+
}
269+
}
270+
251271
/// Simplified version of panic payloads, ignoring
252272
/// types other than `&'static str` and `String`.
253273
pub enum PanicMessage {

0 commit comments

Comments
 (0)