too-many-lists/advanced-lists/intro #1343
Replies: 1 comment 1 reply
-
使用高级技巧:声明forbid_unsafe之后处处unsafe,以此同时获取性能上的优势,以及避开语言律师 #![forbid(unsafe_code)]
#[inline(always)]
fn extend_mut<'a, T>(input: &'a mut T) -> &'static mut T {
struct Bounded<'a, 'b: 'static, T>(&'a mut T, [&'b ();0]);
let mut n:Box<dyn FnMut(&mut T) -> Bounded<'static, '_, T>>=Box::new(|x| Bounded(x, []));
n(input).0
}
pub struct LinkedListImpl<'a:'static,T>{
pub data:T,
pub prev:Option<&'a mut Self>,
pub next:Option<&'a mut Self>,
}
type LinkedList<T>=LinkedListImpl<'static,T>;
impl<T> LinkedList<T> {
pub fn new(t:T)->Self{Self{data:t,prev:None,next:None}}
pub fn insert_next(&mut self, t:&mut Self){
self.next=Some(extend_mut(t));
t.prev=Some(extend_mut(self))
}
pub fn insert_prev(&mut self, t:&mut Self){
t.insert_next(self)
}
// 复杂逻辑我就不写了
}
fn main(){
let mut a=LinkedList::new(3);
let mut b=LinkedList::new(4);
a.insert_next(&mut b);
println!{"{} {}",a.data, a.next.unwrap().data}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
too-many-lists/advanced-lists/intro
https://course.rs/too-many-lists/advanced-lists/intro.html
Beta Was this translation helpful? Give feedback.
All reactions