wangbinyq / wangbinyq.github.io

This is my blog site
https://wangbinyq.github.io
MIT License
1 stars 0 forks source link

rust #2

Open wangbinyq opened 4 years ago

wangbinyq commented 4 years ago
wangbinyq commented 4 years ago

rust 中可以实现下面的方法吗?

pub trait Foo {
    fn bar(self: Rc<dyn Foo>) -> Rc<dyn Foo> {
        Rc::new(BarFoo { foo: self.clone() })
    }
}
wangbinyq commented 4 years ago

how to do placememt new. http://blakesmith.me/2018/12/31/what-is-placement-new-in-rust.html

std::alloc::alloc

wangbinyq commented 4 years ago

std::mem::replace, option::take

空指针在 rust 中的实现:

type EmptyPtr<T> = Option<Rc<T>>;

impl<T> EmptyPtr<T> {
  pub fn is_null(&self) -> bool {
    self.is_none()
  }

  pub fn set_null(&mut self) {
   self.take()
   // std::mem::replace(&mut self, None)
  }
}

unsafe rust, raw pointer

wangbinyq commented 4 years ago
  1. static
  2. singleton