Skip to content

Commit 2feeaf9

Browse files
committed
use new Index trait in IndexThinVec
1 parent e4e2712 commit 2feeaf9

6 files changed

Lines changed: 447 additions & 89 deletions

File tree

crates/dash_middle/src/compiler/constant.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::rc::Rc;
55
use dash_regex::Regex;
66

77
use crate::index_type;
8-
use crate::indexvec::IndexThinVec;
8+
use crate::indexthinvec::IndexThinVec;
99
use crate::interner::Symbol;
1010
use crate::parser::statement::FunctionKind;
1111

@@ -82,11 +82,18 @@ pub struct Function {
8282
pub has_extends_clause: bool,
8383
}
8484

85-
index_type!(NumberConstant u16);
86-
index_type!(BooleanConstant u16);
87-
index_type!(FunctionConstant u16);
88-
index_type!(RegexConstant u16);
89-
index_type!(SymbolConstant u16);
85+
index_type!(
86+
#[derive(Copy, Default, Debug, Clone)]
87+
pub struct NumberConstant(pub u16);
88+
#[derive(Copy, Default, Debug, Clone)]
89+
pub struct BooleanConstant(pub u16);
90+
#[derive(Copy, Default, Debug, Clone)]
91+
pub struct FunctionConstant(pub u16);
92+
#[derive(Copy, Default, Debug, Clone)]
93+
pub struct RegexConstant(pub u16);
94+
#[derive(Copy, Default, Debug, Clone)]
95+
pub struct SymbolConstant(pub u16);
96+
);
9097

9198
#[cfg_attr(feature = "format", derive(serde::Serialize, serde::Deserialize))]
9299
#[derive(Default, Debug, Clone)]
@@ -108,7 +115,10 @@ macro_rules! define_push_methods {
108115
&mut self,
109116
val: $valty,
110117
) -> Result<$constant, LimitExceededError> {
111-
self.$field.try_push(val).map_err(|_| LimitExceededError)
118+
match self.$field.try_push(val) {
119+
Some(index) => Ok(index),
120+
None => Err(LimitExceededError),
121+
}
112122
}
113123
)*
114124
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use std::marker::PhantomData;
2+
3+
use thin_vec::ThinVec;
4+
5+
use crate::indexvec::{Index, IndexRepr};
6+
7+
#[derive(Debug, Clone)]
8+
pub struct IndexThinVec<T, I>(ThinVec<T>, PhantomData<I>);
9+
10+
impl<T, I: Index> IndexThinVec<T, I> {
11+
pub fn new() -> Self {
12+
Self(ThinVec::new(), PhantomData)
13+
}
14+
15+
pub fn try_push(&mut self, element: T) -> Option<I> {
16+
let len = self.0.len();
17+
let index = I::Repr::from_usize_checked(len)?;
18+
self.0.push(element);
19+
Some(I::from_repr(index))
20+
}
21+
22+
pub fn as_slice(&self) -> &[T] {
23+
&self.0
24+
}
25+
}
26+
impl<T, I: Index> std::ops::Index<I> for IndexThinVec<T, I> {
27+
type Output = T;
28+
29+
fn index(&self, index: I) -> &Self::Output {
30+
&self.0[index.into_repr().usize()]
31+
}
32+
}
33+
impl<T, I> Default for IndexThinVec<T, I> {
34+
fn default() -> Self {
35+
Self(ThinVec::default(), PhantomData)
36+
}
37+
}
38+
39+
#[cfg(feature = "format")]
40+
impl<T: serde::Serialize, I> serde::Serialize for IndexThinVec<T, I> {
41+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
42+
where
43+
S: serde::Serializer,
44+
{
45+
use serde::ser::SerializeSeq;
46+
47+
let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
48+
for elem in self.0.iter() {
49+
seq.serialize_element(elem)?;
50+
}
51+
seq.end()
52+
}
53+
}
54+
55+
#[cfg(feature = "format")]
56+
impl<'de, T: serde::Deserialize<'de>, I> serde::Deserialize<'de> for IndexThinVec<T, I> {
57+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
58+
where
59+
D: serde::Deserializer<'de>,
60+
{
61+
struct Vis<T, I>(PhantomData<(T, I)>);
62+
impl<'de, T: serde::Deserialize<'de>, I> serde::de::Visitor<'de> for Vis<T, I> {
63+
type Value = IndexThinVec<T, I>;
64+
65+
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
66+
formatter.write_str("a sequence")
67+
}
68+
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
69+
where
70+
A: serde::de::SeqAccess<'de>,
71+
{
72+
let mut data = ThinVec::with_capacity(seq.size_hint().unwrap_or_default());
73+
while let Some(elem) = seq.next_element::<T>()? {
74+
data.push(elem);
75+
}
76+
Ok(IndexThinVec(data, PhantomData))
77+
}
78+
}
79+
deserializer.deserialize_seq(Vis(PhantomData))
80+
}
81+
}

0 commit comments

Comments
 (0)