contextual

Crates.iocontextual
lib.rscontextual
version0.1.6
sourcesrc
created_at2022-09-01 14:14:43.221076
updated_at2023-04-14 13:31:03.293985
descriptionUtility crate to deal with data in context
homepage
repositoryhttps://github.com/timothee-haudebourg/contextual
max_upload_size
id656720
size20,791
Timothée Haudebourg (timothee-haudebourg)

documentation

https://docs.rs/contextual

README

Contextual

CI Crate informations License Documentation

A small crate to deal with data in context.

use contextual::{WithContext, AsRefWithContext, DisplayWithContext};
use std::fmt;

/// Index of an element in some array.
pub struct Index(usize);

impl<T, C: AsRef<[T]>> AsRefWithContext<T, C> for Index {
  fn as_ref_with<'a>(&'a self, context: &'a C) -> &'a T {
    &context.as_ref()[self.0]
  }
}

impl<'a, C: AsRef<[&'a str]>> DisplayWithContext<C> for Index {
  fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result {
    use fmt::Display;
    context.as_ref()[self.0].fmt(f)
  }
}

let i = Index(1);
let context = ["a", "b", "c"];

print!("index: {}", i.with(&context));
assert_eq!(*i.with(&context).as_ref(), "b")

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 8

cargo fmt