Skip to main content

php_ast/ast/
mod.rs

1mod decls;
2mod exprs;
3mod misc;
4mod names;
5mod stmts;
6
7pub use decls::*;
8pub use exprs::*;
9pub use misc::*;
10pub use names::*;
11pub use stmts::*;
12
13pub(crate) fn is_false(b: &bool) -> bool {
14    !*b
15}
16
17/// Arena-allocated Vec. Thin newtype over bumpalo::collections::Vec that implements Serialize and Debug.
18pub struct ArenaVec<'arena, T>(bumpalo::collections::Vec<'arena, T>);
19
20impl<'arena, T> ArenaVec<'arena, T> {
21    #[inline]
22    pub fn new_in(arena: &'arena bumpalo::Bump) -> Self {
23        Self(bumpalo::collections::Vec::new_in(arena))
24    }
25    #[inline]
26    pub fn with_capacity_in(cap: usize, arena: &'arena bumpalo::Bump) -> Self {
27        Self(bumpalo::collections::Vec::with_capacity_in(cap, arena))
28    }
29    #[inline]
30    pub fn push(&mut self, val: T) {
31        self.0.push(val)
32    }
33    /// Kept as an explicit method so `"ArenaVec::is_empty"` works as a serde
34    /// `skip_serializing_if` path (deref-inherited methods don't resolve via UFCS).
35    #[inline]
36    pub fn is_empty(&self) -> bool {
37        self.0.is_empty()
38    }
39}
40
41impl<'arena, T> IntoIterator for ArenaVec<'arena, T> {
42    type Item = T;
43    type IntoIter = bumpalo::collections::vec::IntoIter<'arena, T>;
44    #[inline]
45    fn into_iter(self) -> Self::IntoIter {
46        self.0.into_iter()
47    }
48}
49
50impl<'arena, T> std::ops::Deref for ArenaVec<'arena, T> {
51    type Target = [T];
52    #[inline]
53    fn deref(&self) -> &[T] {
54        &self.0
55    }
56}
57
58impl<'arena, T> std::ops::DerefMut for ArenaVec<'arena, T> {
59    #[inline]
60    fn deref_mut(&mut self) -> &mut [T] {
61        &mut self.0
62    }
63}
64
65impl<'arena, T: serde::Serialize> serde::Serialize for ArenaVec<'arena, T> {
66    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
67        self.0.as_slice().serialize(s)
68    }
69}
70
71impl<'arena, T: std::fmt::Debug> std::fmt::Debug for ArenaVec<'arena, T> {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        self.0.as_slice().fmt(f)
74    }
75}