proc_macro: support encoding/decoding Vec<T>

This commit is contained in:
Nika Layzell 2021-07-01 12:56:07 -04:00
parent 7678e6ad85
commit 1793ee0658
2 changed files with 35 additions and 0 deletions

View File

@ -337,6 +337,21 @@ impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
}
}
impl<T: Mark> Mark for Vec<T> {
type Unmarked = Vec<T::Unmarked>;
fn mark(unmarked: Self::Unmarked) -> Self {
// Should be a no-op due to std's in-place collect optimizations.
unmarked.into_iter().map(T::mark).collect()
}
}
impl<T: Unmark> Unmark for Vec<T> {
type Unmarked = Vec<T::Unmarked>;
fn unmark(self) -> Self::Unmarked {
// Should be a no-op due to std's in-place collect optimizations.
self.into_iter().map(T::unmark).collect()
}
}
macro_rules! mark_noop {
($($ty:ty),* $(,)?) => {
$(

View File

@ -248,6 +248,26 @@ impl<S> DecodeMut<'_, '_, S> for String {
}
}
impl<S, T: Encode<S>> Encode<S> for Vec<T> {
fn encode(self, w: &mut Writer, s: &mut S) {
self.len().encode(w, s);
for x in self {
x.encode(w, s);
}
}
}
impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T> {
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
let len = usize::decode(r, s);
let mut vec = Vec::with_capacity(len);
for _ in 0..len {
vec.push(T::decode(r, s));
}
vec
}
}
/// Simplified version of panic payloads, ignoring
/// types other than `&'static str` and `String`.
pub enum PanicMessage {