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