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

#1869 in Data structures

Download history 1886/week @ 2024-11-18 1411/week @ 2024-11-25 2175/week @ 2024-12-02 1551/week @ 2024-12-09 1738/week @ 2024-12-16 344/week @ 2024-12-23 389/week @ 2024-12-30 1251/week @ 2025-01-06 2195/week @ 2025-01-13 1862/week @ 2025-01-20 1687/week @ 2025-01-27 1877/week @ 2025-02-03 1803/week @ 2025-02-10 1035/week @ 2025-02-17 1325/week @ 2025-02-24 1486/week @ 2025-03-03

5,713 downloads per month
Used in 87 crates (12 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