Skip to content

Commit 1793ee0

Browse files
committed
proc_macro: support encoding/decoding Vec<T>
1 parent 7678e6a commit 1793ee0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

library/proc_macro/src/bridge/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,21 @@ impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
337337
}
338338
}
339339

340+
impl<T: Mark> Mark for Vec<T> {
341+
type Unmarked = Vec<T::Unmarked>;
342+
fn mark(unmarked: Self::Unmarked) -> Self {
343+
// Should be a no-op due to std's in-place collect optimizations.
344+
unmarked.into_iter().map(T::mark).collect()
345+
}
346+
}
347+
impl<T: Unmark> Unmark for Vec<T> {
348+
type Unmarked = Vec<T::Unmarked>;
349+
fn unmark(self) -> Self::Unmarked {
350+
// Should be a no-op due to std's in-place collect optimizations.
351+
self.into_iter().map(T::unmark).collect()
352+
}
353+
}
354+
340355
macro_rules! mark_noop {
341356
($($ty:ty),* $(,)?) => {
342357
$(

library/proc_macro/src/bridge/rpc.rs

Lines changed: 20 additions & 0 deletions
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<'a, 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)