smt_scope/mem_dbg/
mod.rs

1#[cfg(feature = "mem_dbg")]
2mod r#impl;
3mod utils;
4
5pub use utils::*;
6
7use core::fmt;
8use core::ops::{Deref, DerefMut};
9use std::collections::hash_map::IntoValues;
10
11macro_rules! derive_wrapper {
12    ($head:ident $(:: $tail:ident)+ $(<$($rest1:tt)*)? $(: $($rest2:tt)*)?) => {
13        derive_wrapper!( $head :: ; $($tail,)* $(<$($rest1)*)? $(: $($rest2)*)? );
14    };
15    ($($module:ident ::)+ ; $head:ident , $($tail:ident,)+ $($rest:tt)*) => {
16        derive_wrapper!( $($module ::)* $head :: ; $($tail,)* $($rest)* );
17    };
18    ($($module:ident ::)* ; $struct:ident, $(<$($t:ident$(= $default:ty)?),*>)? $(: $trait:ident $(+ $other:ident)*)?) => {
19        derive_wrapper!(
20            $(#[derive($trait$(,$other)*)])?
21            struct $struct$(<$($t$(= $default)?),*>)?($($module::)+$struct$(<$($t),*>)?);
22        );
23    };
24    (
25        $(#[derive($($d:ident),*)])?
26        struct $struct:ident$(<$($t:ident$(= $default:ty)?$(: $bound:tt)?),*>)?($p:vis $inner:ty);
27    ) => {
28        $(#[derive($($d),*)])?
29        pub struct $struct$(<$($t$(= $default)?$(: $bound)?),*>)?($p $inner);
30        impl$(<$($t$(: $bound)?),*>)? Deref for $struct$(<$($t),*>)? {
31            type Target = $inner;
32            fn deref(&self) -> &Self::Target {
33                &self.0
34            }
35        }
36        impl$(<$($t$(: $bound)?),*>)? DerefMut for $struct$(<$($t),*>)? {
37            fn deref_mut(&mut self) -> &mut Self::Target {
38                &mut self.0
39            }
40        }
41        #[allow(clippy::non_canonical_clone_impl)]
42        impl$(<$($t$(: $bound)?),*>)? Clone for $struct$(<$($t),*>)?
43        where $inner: Clone {
44            fn clone(&self) -> Self {
45                Self(self.0.clone())
46            }
47        }
48        impl$(<$($t$(: $bound)?),*>)? fmt::Debug for $struct$(<$($t),*>)?
49        where $inner: fmt::Debug {
50            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51                self.0.fmt(f)
52            }
53        }
54        #[cfg(feature = "serde")]
55        impl$(<$($t$(: $bound)?),*>)? serde::Serialize for $struct$(<$($t),*>)?
56        where $inner: serde::Serialize {
57            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
58            where
59                S: serde::Serializer,
60            {
61                self.0.serialize(serializer)
62            }
63        }
64        #[cfg(feature = "serde")]
65        impl<'de, $($($t$(: $bound)?),*)?> serde::Deserialize<'de> for $struct$(<$($t),*>)?
66        where $inner: serde::Deserialize<'de> {
67            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
68            where
69                D: serde::Deserializer<'de>,
70            {
71                <$inner as serde::Deserialize<'de>>::deserialize(deserializer).map(Self)
72            }
73        }
74        impl$(<$($t$(: $bound)?),*>)? From<$inner> for $struct$(<$($t),*>)? {
75            fn from(inner: $inner) -> Self {
76                Self(inner)
77            }
78        }
79    };
80}
81
82macro_rules! derive_non_max {
83    ($name:ident, $prim:ident) => {
84        derive_wrapper!(nonmax::$name: Copy + Eq + PartialEq + PartialOrd + Ord + Hash + Default);
85        impl $name {
86            pub const ZERO: Self = Self(nonmax::$name::ZERO);
87            pub const ONE: Self = Self(nonmax::$name::ONE);
88            pub const MAX: Self = Self(nonmax::$name::MAX);
89            pub const fn new(value: $prim) -> Option<Self> {
90                match nonmax::$name::new(value) {
91                    Some(value) => Some(Self(value)),
92                    None => None,
93                }
94            }
95            /// Creates a new non-max without checking the value.
96            ///
97            /// # Safety
98            ///
99            /// The value must not equal the maximum representable value for the
100            /// primitive type.
101            pub const unsafe fn new_unchecked(value: $prim) -> Self {
102                Self(nonmax::$name::new_unchecked(value))
103            }
104            pub const fn get(self) -> $prim {
105                self.0.get()
106            }
107        }
108        impl fmt::Display for $name {
109            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110                self.0.fmt(f)
111            }
112        }
113        impl core::str::FromStr for $name {
114            type Err = <nonmax::$name as core::str::FromStr>::Err;
115            fn from_str(s: &str) -> Result<Self, Self::Err> {
116                nonmax::$name::from_str(s).map(Self)
117            }
118        }
119    };
120}
121
122derive_non_max!(NonMaxU32, u32);
123derive_non_max!(NonMaxUsize, usize);
124
125// BigRational
126
127derive_wrapper!(num::BigRational: PartialEq + Eq + PartialOrd + Ord + Hash);
128
129impl fmt::Display for BigRational {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        self.0.fmt(f)
132    }
133}
134
135// BigUint
136
137derive_wrapper!(num::BigUint: PartialEq + Eq + PartialOrd + Ord + Hash);
138
139impl fmt::Display for BigUint {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        self.0.fmt(f)
142    }
143}
144
145// TiVec
146
147derive_wrapper!(typed_index_collections::TiVec<K, V>);
148impl<K, V> Default for TiVec<K, V> {
149    fn default() -> Self {
150        Self(typed_index_collections::TiVec::default())
151    }
152}
153impl<K, V> FromIterator<V> for TiVec<K, V> {
154    fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
155        Self(typed_index_collections::TiVec::from_iter(iter))
156    }
157}
158impl<K, V> std::iter::IntoIterator for TiVec<K, V> {
159    type Item = <Self::IntoIter as std::iter::IntoIterator>::Item;
160    type IntoIter = std::vec::IntoIter<V>;
161    fn into_iter(self) -> Self::IntoIter {
162        self.0.into_iter()
163    }
164}
165impl<K: From<usize>, V> TiVec<K, V> {
166    pub fn into_iter_enumerated(self) -> impl Iterator<Item = (K, V)> {
167        self.0.into_iter_enumerated()
168    }
169}
170
171// FxHashMap
172
173derive_wrapper!(fxhash::FxHashMap<K, V>);
174impl<K, V> Default for FxHashMap<K, V> {
175    fn default() -> Self {
176        Self(fxhash::FxHashMap::default())
177    }
178}
179impl<K: Eq + std::hash::Hash, V> FromIterator<(K, V)> for FxHashMap<K, V> {
180    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
181        Self(fxhash::FxHashMap::from_iter(iter))
182    }
183}
184impl<K, V> std::iter::IntoIterator for FxHashMap<K, V> {
185    type Item = <Self::IntoIter as std::iter::IntoIterator>::Item;
186    type IntoIter = std::collections::hash_map::IntoIter<K, V>;
187    fn into_iter(self) -> Self::IntoIter {
188        self.0.into_iter()
189    }
190}
191impl<K, V> FxHashMap<K, V> {
192    pub fn into_values(self) -> IntoValues<K, V> {
193        self.0.into_values()
194    }
195}
196
197// FxHashSet
198
199derive_wrapper!(fxhash::FxHashSet<K>);
200impl<K> Default for FxHashSet<K> {
201    fn default() -> Self {
202        Self(fxhash::FxHashSet::default())
203    }
204}
205impl<K: Eq + std::hash::Hash> FromIterator<K> for FxHashSet<K> {
206    fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self {
207        Self(fxhash::FxHashSet::from_iter(iter))
208    }
209}
210impl<K> std::iter::IntoIterator for FxHashSet<K> {
211    type Item = <Self::IntoIter as std::iter::IntoIterator>::Item;
212    type IntoIter = std::collections::hash_set::IntoIter<K>;
213    fn into_iter(self) -> Self::IntoIter {
214        self.0.into_iter()
215    }
216}
217
218// StringTable
219
220derive_wrapper!(
221    // #[derive(Default)]
222    struct StringTable(lasso::Rodeo<lasso::Spur, fxhash::FxBuildHasher>);
223);
224impl StringTable {
225    pub fn with_hasher(hash_builder: fxhash::FxBuildHasher) -> Self {
226        Self(lasso::Rodeo::with_hasher(hash_builder))
227    }
228}
229
230// IString
231
232derive_wrapper!(
233    #[derive(Copy, Default, PartialEq, Eq, Hash)]
234    struct IString(pub lasso::Spur);
235);
236
237// Graph
238
239derive_wrapper!(petgraph::graph::Graph<N, E, Ty = petgraph::Directed, Ix = petgraph::graph::DefaultIx>);
240pub type DiGraph<N, E, Ix = petgraph::graph::DefaultIx> = Graph<N, E, petgraph::Directed, Ix>;
241pub type UnGraph<N, E, Ix = petgraph::graph::DefaultIx> = Graph<N, E, petgraph::Undirected, Ix>;
242impl<N, E, Ty: petgraph::EdgeType, Ix: petgraph::graph::IndexType> Graph<N, E, Ty, Ix> {
243    pub fn with_capacity(nodes: usize, edges: usize) -> Self {
244        Self(petgraph::graph::Graph::<N, E, Ty, Ix>::with_capacity(
245            nodes, edges,
246        ))
247    }
248}