Skip to main content

php_ast/ast/
mod.rs

1mod bytes;
2mod decls;
3mod exprs;
4mod misc;
5mod names;
6mod stmts;
7
8#[cfg(feature = "serde")]
9pub(crate) use bytes::serialize_bytes;
10pub use bytes::ByteStr;
11pub use decls::*;
12pub use exprs::*;
13pub use misc::*;
14pub use names::*;
15pub use stmts::*;
16
17#[cfg(feature = "serde")]
18pub(crate) fn is_false(b: &bool) -> bool {
19    !*b
20}
21
22/// Arena-allocated Vec. Thin newtype over bumpalo::collections::Vec that implements Debug and, with the `serde` feature, Serialize.
23pub struct ArenaVec<'arena, T>(bumpalo::collections::Vec<'arena, T>);
24
25impl<'arena, T> ArenaVec<'arena, T> {
26    /// Creates an empty vector in `arena`.
27    #[inline]
28    pub fn new_in(arena: &'arena bumpalo::Bump) -> Self {
29        Self(bumpalo::collections::Vec::new_in(arena))
30    }
31    /// Creates an empty vector with room for `cap` elements in `arena`.
32    #[inline]
33    pub fn with_capacity_in(cap: usize, arena: &'arena bumpalo::Bump) -> Self {
34        Self(bumpalo::collections::Vec::with_capacity_in(cap, arena))
35    }
36    /// Appends `val` to the end.
37    #[inline]
38    pub fn push(&mut self, val: T) {
39        self.0.push(val)
40    }
41    /// Kept as an explicit method so `"ArenaVec::is_empty"` works as a serde
42    /// `skip_serializing_if` path (deref-inherited methods don't resolve via UFCS).
43    #[inline]
44    pub fn is_empty(&self) -> bool {
45        self.0.is_empty()
46    }
47}
48
49/// The owning iterator returned by [`ArenaVec::into_iter`].
50pub struct IntoIter<'arena, T>(bumpalo::collections::vec::IntoIter<'arena, T>);
51
52impl<'arena, T> Iterator for IntoIter<'arena, T> {
53    type Item = T;
54    #[inline]
55    fn next(&mut self) -> Option<T> {
56        self.0.next()
57    }
58    #[inline]
59    fn size_hint(&self) -> (usize, Option<usize>) {
60        self.0.size_hint()
61    }
62}
63
64impl<'arena, T> DoubleEndedIterator for IntoIter<'arena, T> {
65    #[inline]
66    fn next_back(&mut self) -> Option<T> {
67        self.0.next_back()
68    }
69}
70
71impl<'arena, T> ExactSizeIterator for IntoIter<'arena, T> {}
72
73impl<'arena, T> std::iter::FusedIterator for IntoIter<'arena, T> {}
74
75impl<'arena, T> IntoIterator for ArenaVec<'arena, T> {
76    type Item = T;
77    type IntoIter = IntoIter<'arena, T>;
78    #[inline]
79    fn into_iter(self) -> IntoIter<'arena, T> {
80        IntoIter(self.0.into_iter())
81    }
82}
83
84impl<'arena, T> std::ops::Deref for ArenaVec<'arena, T> {
85    type Target = [T];
86    #[inline]
87    fn deref(&self) -> &[T] {
88        &self.0
89    }
90}
91
92impl<'arena, T> std::ops::DerefMut for ArenaVec<'arena, T> {
93    #[inline]
94    fn deref_mut(&mut self) -> &mut [T] {
95        &mut self.0
96    }
97}
98
99#[cfg(feature = "serde")]
100impl<'arena, T: serde::Serialize> serde::Serialize for ArenaVec<'arena, T> {
101    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
102        self.0.as_slice().serialize(s)
103    }
104}
105
106impl<'arena, T: std::fmt::Debug> std::fmt::Debug for ArenaVec<'arena, T> {
107    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108        self.0.as_slice().fmt(f)
109    }
110}