1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! How a field is handed out by reference.
//!
//! Two traits, each naming ONE projection: a many-valued field is handed out as a slice, an
//! optional one as `Option<&T>`. `#[ingraph::record(…)]` emits an accessor through whichever the
//! declaration marked, writing the field's declared type verbatim into the qualification —
//! `&[<Ids as Repeated>::Item]`.
//!
//! That indirection is the point. The derive used to choose a projection by pattern-matching the
//! written type against `Vec<…>` and `Option<…>`, which a type ALIAS defeated silently: a field
//! declared `files: Ids` was handed back as `&Ids` with nothing to catch it. A macro sees tokens,
//! not resolved types — so the resolution is left to the compiler, which has the type.
//!
//! Implementing either for a container of your own is ordinary trait work: they are `ingraph`'s
//! traits, so `impl ingraph::Repeated for MyVec` is a local trait on a foreign type. A field
//! marked for a projection its type cannot satisfy fails as an unsatisfied bound naming the trait.
//!
//! Each trait projects BOTH WAYS. The derive emits no `*_mut` accessor — handing out `&mut` to one
//! column would let a caller mutate a live map key in place — so the mutable halves are here for
//! hand-written use, and so that a container's own impl is complete. A projection that could only
//! borrow immutably would send its owner reaching past the trait to mutate, which is the seam the
//! traits exist to remove.
#[cfg(test)]
mod tests;
/// A field holding many values of one item type, handed out as a slice.
pub trait Repeated {
/// What the field holds many of.
type Item;
/// The items, contiguously.
fn as_slice(&self) -> &[Self::Item];
/// The items, mutably.
///
/// Nothing the derive emits calls this — see the module doc. It is the half a container's owner
/// needs, not the half an accessor uses.
fn as_mut_slice(&mut self) -> &mut [Self::Item];
}
/// A field that may hold one value, handed out as `Option<&T>` rather than `&Option<T>` — the
/// shape a caller can use without a second step.
pub trait Optional {
/// What the field may hold.
type Item;
/// The value, borrowed in place.
fn as_option(&self) -> Option<&Self::Item>;
/// The value, borrowed mutably in place.
///
/// Nothing the derive emits calls this — see the module doc. It is the half a container's owner
/// needs, not the half an accessor uses.
fn as_option_mut(&mut self) -> Option<&mut Self::Item>;
}
impl<T> Repeated for Vec<T> {
type Item = T;
#[inline]
fn as_slice(&self) -> &[T] {
self
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] {
self
}
}
impl<T> Optional for Option<T> {
type Item = T;
#[inline]
fn as_option(&self) -> Option<&T> {
self.as_ref()
}
#[inline]
fn as_option_mut(&mut self) -> Option<&mut T> {
self.as_mut()
}
}