ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
//! 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()
  }
}