Skip to content

Commit 260b725

Browse files
committed
Test implementations of GcDeserialize
1 parent e685b2a commit 260b725

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ zerogc-derive = { path = "libs/derive", version = "0.2.0-alpha.5" }
2323
# Used for the "epsilon" no-op collector
2424
bumpalo = { version = "3", optional = true }
2525

26+
[dev-dependencies]
27+
serde_json = "1"
28+
# Used to test custom hash function
29+
# support for IndexMap
30+
fnv = "1"
31+
2632
[workspace]
2733
members = ["libs/simple", "libs/derive", "libs/context"]
2834

src/macros.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/// ## Example
66
/// ```
77
/// # use zerogc::{Trace, DynTrace, trait_object_trace};
8-
/// # use zerogc_derive::Trace;
98
/// # use zerogc::epsilon::{self, EpsilonCollectorId, Gc};
109
/// # type OurSpecificId = EpsilonCollectorId;
1110
/// trait Foo<'gc>: DynTrace<'gc, OurSpecificId> {
@@ -113,4 +112,4 @@ macro_rules! impl_trace_for_nulltrace {
113112
}
114113
}
115114
}
116-
}
115+
}

src/manually_traced/indexmap.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ unsafe_gc_impl! {
1212
TraceImmutable => { where K: TraceImmutable, V: TraceImmutable, S: 'static },
1313
TrustedDrop => { where K: TrustedDrop, V: TrustedDrop, S: 'static },
1414
GcSafe => { where K: GcSafe<'gc, Id>, V: GcSafe<'gc, Id>, S: 'static },
15-
GcDeserialize => { where K: GcDeserialize<'gc, 'de, Id>, V: GcDeserialize<'gc, 'de, Id>, S: 'static }
1615
},
1716
null_trace => { where K: NullTrace, V: NullTrace },
1817
NEEDS_TRACE => K::NEEDS_TRACE || V::NEEDS_TRACE,
@@ -47,7 +46,6 @@ unsafe_gc_impl! {
4746
TraceImmutable => { where T: TraceImmutable, S: 'static },
4847
TrustedDrop => { where T: TrustedDrop, S: 'static },
4948
GcSafe => { where T: GcSafe<'gc, Id>, S: 'static },
50-
GcDeserialize => { where K: GcDeserialize<'gc, 'de, Id>, V: GcDeserialize<'gc, 'de, Id>, S: 'static },
5149
},
5250
NEEDS_TRACE => T::NEEDS_TRACE,
5351
NEEDS_DROP => true, // Internal memory

src/serde.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,43 @@ impl_for_set!(HashSet<T, S> where T: TraceImmutable, S: 'static);
272272
impl_for_map!(IndexMap<K, V, S> where K: TraceImmutable, S: 'static);
273273
#[cfg(feature = "indexmap")]
274274
impl_for_set!(IndexSet<T, S> where T: TraceImmutable, S: 'static);
275+
276+
#[cfg(test)]
277+
mod test {
278+
use crate::epsilon::{EpsilonSystem, EpsilonCollectorId};
279+
use super::*;
280+
#[test]
281+
#[cfg(feature = "indexmap")]
282+
fn indexmap() {
283+
let system = EpsilonSystem::leak();
284+
let ctx = system.new_context();
285+
const INPUT: &str = r##"{"foo": "bar", "eats": "turds"}"##;
286+
let mut deser = serde_json::Deserializer::from_str(INPUT);
287+
let s = |s: &'static str| String::from(s);
288+
assert_eq!(
289+
<IndexMap<String, Gc<String, EpsilonCollectorId>> as GcDeserialize<EpsilonCollectorId>>::deserialize_gc(&ctx, &mut deser).unwrap(),
290+
indexmap::indexmap!(
291+
s("foo") => ctx.alloc(s("bar")),
292+
s("eats") => ctx.alloc(s("turds"))
293+
)
294+
);
295+
let mut deser = serde_json::Deserializer::from_str(INPUT);
296+
assert_eq!(
297+
<IndexMap<String, Gc<String, EpsilonCollectorId>, fnv::FnvBuildHasher> as GcDeserialize<EpsilonCollectorId>>::deserialize_gc(&ctx, &mut deser).unwrap(),
298+
indexmap::indexmap!(
299+
s("foo") => ctx.alloc(s("bar")),
300+
s("eats") => ctx.alloc(s("turds"))
301+
)
302+
);
303+
}
304+
#[test]
305+
fn gc() {
306+
let system = EpsilonSystem::leak();
307+
let ctx = system.new_context();
308+
let mut deser = serde_json::Deserializer::from_str(r#"128"#);
309+
assert_eq!(
310+
<Gc<i32, EpsilonCollectorId> as GcDeserialize<EpsilonCollectorId>>::deserialize_gc(&ctx, &mut deser).unwrap(),
311+
ctx.alloc(128)
312+
);
313+
}
314+
}

0 commit comments

Comments
 (0)