creature_feature/
whole_empty.rs

1use crate::accum_ftzr::{Ftzr, IterFtzr, LinearFixed};
2use crate::feature_from::FeatureFrom;
3use crate::internal::impl_ftrzs;
4use std::ops::Deref;
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9/// The struct created by `whole()`
10#[derive(Hash, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Debug)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub struct Whole;
13
14#[derive(Hash, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Debug)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16pub(crate) struct WholeAtom<T>(pub T);
17
18impl<T> Deref for WholeAtom<T> {
19    type Target = T;
20    fn deref(self: &Self) -> &T {
21        &self.0
22    }
23}
24
25impl<'a, T> FeatureFrom<WholeAtom<T>> for &'a str
26where
27    &'a str: FeatureFrom<T>,
28{
29    fn from(t: WholeAtom<T>) -> &'a str {
30        FeatureFrom::from(t.0)
31    }
32}
33impl<'a, T, U> FeatureFrom<WholeAtom<T>> for &'a [U]
34where
35    &'a [U]: FeatureFrom<T>,
36{
37    fn from(t: WholeAtom<T>) -> &'a [U] {
38        FeatureFrom::from(t.0)
39    }
40}
41impl<'a, T, U, const N: usize> FeatureFrom<WholeAtom<T>> for &'a [U; N]
42where
43    &'a [U; N]: FeatureFrom<T>,
44{
45    fn from(t: WholeAtom<T>) -> &'a [U; N] {
46        FeatureFrom::from(t.0)
47    }
48}
49impl<T, U, const N: usize> FeatureFrom<WholeAtom<T>> for [U; N]
50where
51    [U; N]: FeatureFrom<T>,
52{
53    fn from(t: WholeAtom<T>) -> [U; N] {
54        FeatureFrom::from(t.0)
55    }
56}
57
58impl<T> FeatureFrom<WholeAtom<T>> for String
59where
60    String: FeatureFrom<T>,
61{
62    fn from(t: WholeAtom<T>) -> String {
63        FeatureFrom::from(t.0)
64    }
65}
66
67impl<T, Q: IntoIterator<Item = T>> FeatureFrom<WholeAtom<Q>> for Vec<T> {
68    fn from(t: WholeAtom<Q>) -> Vec<T> {
69        Iterator::collect(t.0.into_iter())
70    }
71}
72
73impl<'a, T> IterFtzr<&'a [T]> for Whole {
74    type TokenGroup = &'a [T];
75    type Iter = std::option::IntoIter<Self::TokenGroup>;
76    fn iterate_features(&self, origin: &'a [T]) -> Self::Iter {
77        Some(origin).into_iter()
78    }
79}
80impl_ftrzs!(Whole);
81
82/// The featurizer returned by `empty()`
83#[derive(Hash, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Debug)]
84#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
85pub struct Empty;
86
87/// The type of <Empty as Ftzr<T>>::TokenGroup, should actually be unreachable and will never be produced or consumed.
88#[derive(Hash, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Debug)]
89#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
90pub struct EmptyAtom;
91
92impl FeatureFrom<EmptyAtom> for String {
93    fn from(x: EmptyAtom) -> Self {
94        Default::default()
95    }
96}
97
98impl<T> FeatureFrom<EmptyAtom> for Option<T> {
99    fn from(x: EmptyAtom) -> Self {
100        Default::default()
101    }
102}
103
104impl LinearFixed for Empty {
105    fn chunk_size(&self) -> usize {
106        0
107    }
108}
109
110impl<'a, T> IterFtzr<&'a [T]> for Empty {
111    type TokenGroup = EmptyAtom;
112    type Iter = std::option::IntoIter<Self::TokenGroup>;
113    fn iterate_features(&self, origin: &'a [T]) -> Self::Iter {
114        None.into_iter()
115    }
116}
117/// The identity featurizer. `whole().featurize(your_data)` will be isomorphic to your original data. For example, `Vec<&str>` would be a one-length vector containing the original string. Particularly useful with `bookends` and `for_each`.
118pub fn whole() -> Whole {
119    Whole
120}
121
122/// The empty featurizer. It will never produce anything. All collections produce by it will be equal to `Default::default()`.
123pub fn empty() -> Empty {
124    Empty
125}
126impl_ftrzs!(Empty);