Skip to content
Snippets Groups Projects
Commit 258e42ad authored by eta's avatar eta
Browse files

Merge branch 'slice-from-ref' into 'main'

Simpify tor-netdoc TokVal

See merge request tpo/core/arti!404
parents ff489ffd 888eea47
No related branches found
No related tags found
No related merge requests found
...@@ -3439,6 +3439,7 @@ dependencies = [ ...@@ -3439,6 +3439,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",
......
...@@ -42,6 +42,7 @@ bitflags = "1" ...@@ -42,6 +42,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"] }
......
...@@ -18,6 +18,8 @@ use crate::parse::rules::*; ...@@ -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,58 +38,37 @@ pub(crate) struct SectionRules<T: Keyword> { ...@@ -36,58 +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.
///
/// We use a one-element array here so that we can return a slice
/// of the array.
Some([Item<'a, K>; 1]),
/// 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) => &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]),
}
} }
} }
...@@ -111,7 +92,7 @@ impl<'a, T: Keyword> Section<'a, T> { ...@@ -111,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,
...@@ -169,19 +150,9 @@ impl<'a, T: Keyword> Section<'a, T> { ...@@ -169,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);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment