tor-netdoc: Actually, make TokVal be a Vec

This enum was otiose: its set of valid values is precisely those of a
Vec.  (Indeed what would TokVal::Multi(vec![]) have meant?)
This commit is contained in:
Ian Jackson 2022-03-11 18:42:01 +00:00
parent 9a7783a230
commit 888eea47b3
3 changed files with 22 additions and 46 deletions

1
Cargo.lock generated
View File

@ -3441,6 +3441,7 @@ dependencies = [
"bitflags", "bitflags",
"derive_more", "derive_more",
"digest 0.10.3", "digest 0.10.3",
"educe",
"hex", "hex",
"hex-literal", "hex-literal",
"once_cell", "once_cell",

View File

@ -36,6 +36,7 @@ bitflags = "1"
time = { version = "0.3", features = ["std", "parsing", "macros"] } time = { version = "0.3", features = ["std", "parsing", "macros"] }
derive_more = "0.99" derive_more = "0.99"
digest = "0.10.0" digest = "0.10.0"
educe = "0.4.6"
hex = "0.4" hex = "0.4"
once_cell = "1" once_cell = "1"
phf = { version = "0.10.0", features = ["macros"] } phf = { version = "0.10.0", features = ["macros"] }

View File

@ -18,6 +18,8 @@ use crate::parse::rules::*;
use crate::parse::tokenize::*; use crate::parse::tokenize::*;
use crate::{ParseErrorKind as EK, Result}; use crate::{ParseErrorKind as EK, Result};
use educe::Educe;
/// Describe the rules for one section of a document. /// Describe the rules for one section of a document.
/// ///
/// The rules are represented as a mapping from token index to /// The rules are represented as a mapping from token index to
@ -36,55 +38,37 @@ pub(crate) struct SectionRules<T: Keyword> {
} }
/// The entry or entries for a particular keyword within a document. /// The entry or entries for a particular keyword within a document.
#[derive(Clone)] #[derive(Clone, Educe)]
enum TokVal<'a, K: Keyword> { #[educe(Default)]
/// No value has been found. struct TokVal<'a, K: Keyword>(Vec<Item<'a, K>>);
None,
/// A single value has been found; we're storing it in place.
Some(Item<'a, K>),
/// Multiple values have been found; they go in a vector.
Multi(Vec<Item<'a, K>>),
}
impl<'a, K: Keyword> TokVal<'a, K> { impl<'a, K: Keyword> TokVal<'a, K> {
/// Return the number of Items for this value.
fn none() -> Self {
Default::default()
}
/// Return the number of Items for this value. /// Return the number of Items for this value.
fn count(&self) -> usize { fn count(&self) -> usize {
match self { self.0.len()
TokVal::None => 0,
TokVal::Some(_) => 1,
TokVal::Multi(v) => v.len(),
}
} }
/// Return the first Item for this value, or None if there wasn't one. /// Return the first Item for this value, or None if there wasn't one.
fn first(&self) -> Option<&Item<'a, K>> { fn first(&self) -> Option<&Item<'a, K>> {
match self { self.0.get(0)
TokVal::None => None,
TokVal::Some(t) => Some(t),
TokVal::Multi(v) => Some(&v[0]),
}
} }
/// Return the Item for this value, if there is exactly one. /// Return the Item for this value, if there is exactly one.
fn singleton(&self) -> Option<&Item<'a, K>> { fn singleton(&self) -> Option<&Item<'a, K>> {
match self { match &*self.0 {
TokVal::None => None, &[ref x] => Some(x),
TokVal::Some(t) => Some(t), _ => None,
TokVal::Multi(_) => None,
} }
} }
/// Return all the Items for this value, as a slice. /// Return all the Items for this value, as a slice.
fn as_slice(&self) -> &[Item<'a, K>] { fn as_slice(&self) -> &[Item<'a, K>] {
match self { &self.0
TokVal::None => &[],
TokVal::Some(t) => std::slice::from_ref(t),
TokVal::Multi(v) => &v[..],
}
} }
/// Return the last Item for this value, if any. /// Return the last Item for this value, if any.
fn last(&self) -> Option<&Item<'a, K>> { fn last(&self) -> Option<&Item<'a, K>> {
match self { self.0.last()
TokVal::None => None,
TokVal::Some(t) => Some(t),
TokVal::Multi(v) => Some(&v[v.len() - 1]),
}
} }
} }
@ -108,7 +92,7 @@ impl<'a, T: Keyword> Section<'a, T> {
fn new() -> Self { fn new() -> Self {
let n = T::n_vals(); let n = T::n_vals();
let mut v = Vec::with_capacity(n); let mut v = Vec::with_capacity(n);
v.resize(n, TokVal::None); v.resize(n, TokVal::none());
Section { Section {
v, v,
first: None, first: None,
@ -166,19 +150,9 @@ impl<'a, T: Keyword> Section<'a, T> {
fn add_tok(&mut self, t: T, item: Item<'a, T>) { fn add_tok(&mut self, t: T, item: Item<'a, T>) {
let idx = Keyword::idx(t); let idx = Keyword::idx(t);
if idx >= self.v.len() { if idx >= self.v.len() {
self.v.resize(idx + 1, TokVal::None); self.v.resize(idx + 1, TokVal::none());
} }
let m = &mut self.v[idx]; self.v[idx].0.push(item);
match m {
TokVal::None => *m = TokVal::Some(item),
TokVal::Some(x) => {
*m = TokVal::Multi(vec![x.clone(), item]);
}
TokVal::Multi(ref mut v) => {
v.push(item);
}
};
if self.first.is_none() { if self.first.is_none() {
self.first = Some(t); self.first = Some(t);
} }