Skip to content

Commit 3140b05

Browse files
committed
impl GcDeserialize for GcString and GcArray
1 parent 83644f3 commit 3140b05

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/serde.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
//! that requires a [GcContext].
1212
use std::marker::PhantomData;
1313

14-
use serde::de::{Deserializer, DeserializeSeed};
14+
use crate::array::{GcArray, GcString};
15+
use serde::de::{self, Deserializer, DeserializeSeed};
1516

1617
use crate::prelude::*;
1718

@@ -35,6 +36,36 @@ impl<'gc, 'de, Id: CollectorId, T: GcDeserialize<'gc, 'de, Id>> GcDeserialize<'g
3536
}
3637
}
3738

39+
40+
impl<'gc, 'de, Id: CollectorId, T: GcDeserialize<'gc, 'de, Id>> GcDeserialize<'gc, 'de, Id> for GcArray<'gc, T, Id>
41+
where <Id::System as GcSystem>::Context: GcSimpleAlloc {
42+
fn deserialize_gc<D: Deserializer<'de>>(ctx: &'gc <Id::System as GcSystem>::Context, deserializer: D) -> Result<Self, D::Error> {
43+
Ok(ctx.alloc_array_from_vec(
44+
Vec::<T>::deserialize_gc(ctx, deserializer)?
45+
))
46+
}
47+
}
48+
49+
50+
impl<'gc, 'de, Id: CollectorId> GcDeserialize<'gc, 'de, Id> for GcString<'gc, Id>
51+
where <Id::System as GcSystem>::Context: GcSimpleAlloc {
52+
fn deserialize_gc<D: Deserializer<'de>>(ctx: &'gc <Id::System as GcSystem>::Context, deserializer: D) -> Result<Self, D::Error> {
53+
struct GcStrVisitor<'gc, A: GcSimpleAlloc> {
54+
ctx: &'gc A
55+
}
56+
impl<'de, 'gc, A: GcSimpleAlloc> de::Visitor<'de> for GcStrVisitor<'gc, A> {
57+
type Value = GcString<'gc, A::Id>;
58+
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
59+
f.write_str("a string")
60+
}
61+
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: de::Error, {
62+
Ok(self.ctx.alloc_str(v))
63+
}
64+
}
65+
deserializer.deserialize_str(GcStrVisitor { ctx })
66+
}
67+
}
68+
3869
/// Implement [GcDeserialize] for a type by delegating to its [serde::Deserialize] implementation.
3970
///
4071
/// This should only be used for types that can never have gc pointers inside of them (or if you don't care to support that).

0 commit comments

Comments
 (0)