#context #trait

contextual

Utility crate to deal with data in context

5 releases

0.1.4 Dec 16, 2022
0.1.3 Sep 1, 2022
0.1.2 Sep 1, 2022
0.1.1 Sep 1, 2022
0.1.0 Sep 1, 2022

#815 in Data structures

Download history 65/week @ 2022-11-26 89/week @ 2022-12-03 98/week @ 2022-12-10 115/week @ 2022-12-17 48/week @ 2022-12-24 24/week @ 2022-12-31 113/week @ 2023-01-07 95/week @ 2023-01-14 113/week @ 2023-01-21 109/week @ 2023-01-28 102/week @ 2023-02-04 138/week @ 2023-02-11 314/week @ 2023-02-18 292/week @ 2023-02-25 320/week @ 2023-03-04 135/week @ 2023-03-11

1,102 downloads per month
Used in 29 crates (10 directly)

MIT/Apache

8KB
102 lines

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.

No runtime deps