-
Rust)Yet another linked list question about RefCell.코딩Coding/★Rust❤️Parallelism_Concurrency★ 2022. 4. 18. 23:02728x90
Yet another linked list question about RefCell.
Hi! Can you suggest a fix? I have a singly linked list of `Option<Rc<RefCell<Node<T>>>>`, and I'm trying to return a `core::cell::Ref` from the...
www.reddit.com
Yet another linked list question about RefCell.
Hi! Can you suggest a fix? I have a singly linked list of `Option<Rc<RefCell<Node<T>>>>`, and I'm trying to return a `core::cell::Ref` from the middle of it. Is there a way to do it without being limited by the lifetime of a local variable?Yet another linked list question about RefCell. Hi! Can you suggest a fix? I have a singly linked list of `Option<Rc<RefCell<Node<T>>>>`, and I'm trying to return a `core::cell::Ref` from the middle of it. Is there a way to do it without being limited by the lifetime of a local variable? #[derive(Debug)] pub struct List<T> { maybe_head: Option<Rc<RefCell<Node<T>>>>, maybe_tail: Option<Rc<RefCell<Node<T>>>>, length: usize, } #[derive(Debug)] struct Node<T> { value: T, maybe_next: Option<Rc<RefCell<Node<T>>>>, } impl<T> List<T> { fn get<'a>(&'a self, ind: usize) -> Box<dyn Deref<Target = T> + 'a> { if self.length == 0 || ind < 0 || ind >= self.length { panic!("Index out of bounds!"); } if ind == 0 { let a = self.maybe_head.as_ref().unwrap(); let b = a.borrow(); let c = Ref::map(b, |d| &d.value); return Box::new(c); } let mut opt: Option<Rc<RefCell<Node<T>>>> = Some(Rc::clone(self.maybe_head.as_ref().unwrap())); let mut rc: Rc<RefCell<Node<T>>>; let mut borrowed_node: Ref<Node<T>>; let mut countdown = ind; #[allow(unused_must_use)] loop { rc = Rc::clone(opt.as_ref().unwrap()); borrowed_node = rc.borrow(); if countdown == 0 { let tmp = Ref::map(borrowed_node, |node| &node.value); return Box::new(tmp); } else { let tmp1 = borrowed_node.maybe_next.as_ref().unwrap(); let tmp2 = Rc::clone(tmp1); opt.replace(tmp2); countdown -= 1; } } } }
#[derive(Debug)]
pub struct List<T> {
maybe_head: Option<Rc<RefCell<Node<T>>>>,
maybe_tail: Option<Rc<RefCell<Node<T>>>>,
length: usize,
}
#[derive(Debug)]
struct Node<T> {
value: T,
maybe_next: Option<Rc<RefCell<Node<T>>>>,
}
impl<T> List<T> {
fn get<'a>(&'a self, ind: usize) -> Box<dyn Deref<Target = T> + 'a> {
if self.length == 0 || ind < 0 || ind >= self.length {
panic!("Index out of bounds!");
}
if ind == 0 {
let a = self.maybe_head.as_ref().unwrap();
let b = a.borrow();
let c = Ref::map(b, |d| &d.value);
return Box::new(c);
}
let mut opt: Option<Rc<RefCell<Node<T>>>> = Some(Rc::clone(self.maybe_head.as_ref().unwrap()));
let mut rc: Rc<RefCell<Node<T>>>;
let mut borrowed_node: Ref<Node<T>>;
let mut countdown = ind;
#[allow(unused_must_use)]
loop {
rc = Rc::clone(opt.as_ref().unwrap());
borrowed_node = rc.borrow();
if countdown == 0 {
let tmp = Ref::map(borrowed_node, |node| &node.value);
return Box::new(tmp);
} else {
let tmp1 = borrowed_node.maybe_next.as_ref().unwrap();
let tmp2 = Rc::clone(tmp1);
opt.replace(tmp2);
countdown -= 1;
}
}
}
}반응형'코딩Coding > ★Rust❤️Parallelism_Concurrency★' 카테고리의 다른 글
Rust❤️)Stefan Schindler: Parallel Programming with Thread pools and iterators (0) 2022.06.25 Concurrency동시성 vs Parallelism병렬성 이해하기 (0) 2022.06.18 Rust❤️Async I/O Event polls & State Machines (0) 2022.06.13 du it in Rust: async, tokio, streams, and surprises about perf (0) 2022.06.10 Psst: 3rd-party Spotify client built with Rust and Druid (0) 2021.01.03 Rust]A look at tokio 1.0 API changes (0) 2020.12.31 (Rust) The piet-gpu vision (0) 2020.12.11 async-std v1.8.0 has been released! (0) 2020.12.09