Skip to content

Commit 41b8c70

Browse files
committed
Add clear function to HashMapContext.
1 parent f8880fb commit 41b8c70

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/context/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ impl HashMapContext {
209209
pub fn clear_functions(&mut self) {
210210
self.functions.clear()
211211
}
212+
213+
/// Removes all variables and functions from the context.
214+
/// This allows to reuse the context without allocating a new HashMap.
215+
///
216+
/// # Example
217+
///
218+
/// ```rust
219+
/// # use evalexpr::*;
220+
///
221+
/// let mut context = HashMapContext::new();
222+
/// context.set_value("abc".into(), "def".into()).unwrap();
223+
/// assert_eq!(context.get_value("abc"), Some(&("def".into())));
224+
/// context.clear();
225+
/// assert_eq!(context.get_value("abc"), None);
226+
/// ```
227+
pub fn clear(&mut self) {
228+
self.clear_variables();
229+
self.clear_functions();
230+
}
212231
}
213232

214233
impl Context for HashMapContext {

0 commit comments

Comments
 (0)