Skip to main content

use_containment/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4/// A simple inside, boundary, or outside classification.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Containment {
7    /// Strictly inside the containing value.
8    Inside,
9    /// On the boundary of the containing value.
10    Boundary,
11    /// Outside the containing value.
12    Outside,
13}
14
15impl Containment {
16    /// Returns `true` for inside or boundary classifications.
17    #[must_use]
18    pub const fn is_contained(self) -> bool {
19        matches!(self, Self::Inside | Self::Boundary)
20    }
21
22    /// Returns `true` when the classification is strictly outside.
23    #[must_use]
24    pub const fn is_outside(self) -> bool {
25        matches!(self, Self::Outside)
26    }
27}
28
29/// A trait for values that can classify containment of `T`.
30pub trait Contains<T> {
31    /// Returns the containment classification for `value`.
32    fn contains_value(&self, value: &T) -> Containment;
33}
34
35#[cfg(test)]
36mod tests {
37    use super::Containment;
38
39    #[test]
40    fn classifies_containment() {
41        assert!(Containment::Inside.is_contained());
42        assert!(Containment::Boundary.is_contained());
43        assert!(!Containment::Outside.is_contained());
44        assert!(Containment::Outside.is_outside());
45    }
46}