Guest User

Untitled

a guest
Jul 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. use std::cell::RefCell;
  2.  
  3. struct Foo<'a> {
  4. x: RefCell<Option<&'a mut [u8]>>
  5. }
  6.  
  7. impl<'a> Foo<'a> {
  8. fn set(&self, arr: &'a mut [u8]) {
  9. *self.x.borrow_mut() = Some(arr);
  10. }
  11.  
  12. fn remove(&self) -> &'a mut [u8] {
  13. self.x.borrow_mut().take().unwrap()
  14. }
  15.  
  16. }
  17.  
  18. pub fn main(){
  19. let foo = Foo {x: RefCell::new(None)};
  20. let mut arr = Vec::with_capacity(100).into_boxed_slice();
  21.  
  22. {
  23. foo.set(&mut arr);
  24.  
  25. // тут куча кода, который может при надобности обращаться
  26. // к полю массиву в структуре foo
  27. foo.get();
  28.  
  29. }
  30.  
  31. }
Add Comment
Please, Sign In to add comment