match_string/
dest.rs

1use std::cell::{RefCell, RefMut};
2
3/// A destination that can store matched items.
4/// This is a wrapper around `RefCell<T>` to allow interior mutability
5/// while providing a convenient API for pattern matching destinations.
6#[derive(Default)]
7pub struct Dest<T> {
8    inner: RefCell<T>,
9}
10
11impl<T> Dest<T> {
12    pub fn new() -> Self
13    where
14        T: Default,
15    {
16        Default::default()
17    }
18    pub fn borrow_mut(&self) -> RefMut<'_, T> {
19        self.inner.borrow_mut()
20    }
21
22    pub fn as_refcell(&self) -> &RefCell<T> {
23        &self.inner
24    }
25
26    pub fn into_inner(self) -> T {
27        self.inner.into_inner()
28    }
29}
30
31impl<T> From<T> for Dest<T> {
32    fn from(value: T) -> Self {
33        Dest {
34            inner: RefCell::new(value),
35        }
36    }
37}
38
39impl<T> Clone for Dest<T>
40where
41    T: Clone,
42{
43    fn clone(&self) -> Self {
44        self.inner.borrow().clone().into()
45    }
46}
47
48impl<T> std::fmt::Debug for Dest<T>
49where
50    T: std::fmt::Debug,
51{
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.debug_tuple("Dest").field(&self.inner.borrow()).finish()
54    }
55}
56
57impl<Item, T> crate::base::Destination<Item> for Dest<T>
58where
59    T: crate::base::Destination<Item>,
60{
61    fn pickup(&mut self, item: Item) {
62        self.inner.borrow_mut().pickup(item)
63    }
64}