is_tree/traits/
has_branch.rs

1use crate::longer_mut;
2use super::{AddBranch, HasBranches, HasGet, HasGetMut, HasPathSegment};
3
4// TODO: Add tests for this.
5pub trait HasBranch<'a> {
6    fn branch<T>(&'a mut self, segment: impl Into<String>) -> &'a mut T
7    where &'a mut Self: HasGet + HasBranches<&'a mut T>,
8          Self: AddBranch<T> + Sized + HasGetMut<'a>,
9          T: HasPathSegment + 'a,
10          String: Into<T>
11    {
12        let segment = segment.into();
13        let self_ = unsafe { longer_mut(self) }; // This is safe.
14        if let Some(value) = self.get_mut::<&mut T>(segment.clone()) {
15            value
16        } else {
17            self_.add_branch(segment.into())
18        }
19    }
20}
21
22impl<'a, T> HasBranch<'a> for T {}