7 releases

0.1.6 Apr 14, 2023
0.1.5 Apr 14, 2023
0.1.4 Dec 16, 2022
0.1.3 Sep 1, 2022

#1515 in Data structures

Download history 1759/week @ 2024-09-28 2220/week @ 2024-10-05 1718/week @ 2024-10-12 1757/week @ 2024-10-19 1543/week @ 2024-10-26 1811/week @ 2024-11-02 1303/week @ 2024-11-09 1819/week @ 2024-11-16 1397/week @ 2024-11-23 2121/week @ 2024-11-30 1636/week @ 2024-12-07 1777/week @ 2024-12-14 390/week @ 2024-12-21 321/week @ 2024-12-28 1196/week @ 2025-01-04 1734/week @ 2025-01-11

4,016 downloads per month
Used in 76 crates (13 directly)

MIT/Apache

9KB
152 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