Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::cell::{RefCell, RefMut, Ref};
- #[derive(Debug)]
- struct SharedRcOption<T>(Option<Rc<RefCell<T>>>);
- impl<T> SharedRcOption<T> {
- fn empty() -> Self {
- Self(None)
- }
- fn new(element: T) -> Self {
- Self(Some(Rc::new(RefCell::new(element))))
- }
- fn is_empty(&self) -> bool {
- self.0.is_none()
- }
- fn borrow_mut(&mut self) -> RefMut<T> {
- self.0.as_mut().expect(
- "Empty SharedRCOption cannot be unwrapped"
- ).borrow_mut()
- }
- fn borrow(&self) -> Ref<T> {
- self.0.as_ref().expect(
- "Empty SharedRCOption cannot be unwrapped"
- ).borrow()
- }
- fn clear(&mut self) {
- self.0 = None;
- }
- }
- impl<T> Clone for SharedRcOption<T> {
- fn clone(&self) -> Self {
- Self(self.0.clone())
- }
- }
- #[derive(Debug)]
- struct Edge {
- next: SharedRcOption<Edge>,
- }
- impl Edge {
- fn new() -> Self {
- Self {
- next: SharedRcOption::empty(),
- }
- }
- fn as_shared_rc_option(self) -> SharedRcOption<Self> {
- SharedRcOption::new(self)
- }
- }
- fn main() {
- let mut a = Edge::new().as_shared_rc_option();
- a.borrow_mut().next = Edge::new().as_shared_rc_option();
- a.borrow_mut().next.borrow_mut().next = Edge::new().as_shared_rc_option();
- a.borrow_mut().next.borrow_mut().next.borrow_mut().next = Edge::new().as_shared_rc_option();
- a.borrow_mut().next.borrow_mut().next.borrow_mut().next.borrow_mut().next =
- a.clone();
- //a.next.borrow_mut().next.clear();
- println!("{:?}", a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement