Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust #2

Open
wangbinyq opened this issue Nov 14, 2019 · 4 comments
Open

rust #2

wangbinyq opened this issue Nov 14, 2019 · 4 comments

Comments

@wangbinyq
Copy link
Owner

wangbinyq commented Nov 14, 2019

  • ownership, borrower-checker, life time; 如果只是处理业务逻辑, 正常使用 safe rust 基本上没有太大问题, 出现问题根据编译器提示修改就行了.
  • 操作符重载, left right
  • &, Box, Rc 指针
    • &
    • Box::clone 会同时 clone 内部对象
    • Rc::clone 修改引用计数
    • perfer Rc
  • 多态: 'a dyn trait => Rc
  • thread: Rc -> Arc, Trait: Send + Sync
@wangbinyq
Copy link
Owner Author

wangbinyq commented Nov 14, 2019

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

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

@wangbinyq
Copy link
Owner Author

wangbinyq commented Nov 14, 2019

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

std::alloc::alloc

@wangbinyq
Copy link
Owner Author

wangbinyq commented Nov 15, 2019

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
Copy link
Owner Author

  1. static
  2. singleton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant