Guest User

Untitled

a guest
Jan 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. mod final_impl {
  2. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
  3. #[repr(transparent)]
  4. pub struct Final<T>(T);
  5.  
  6. impl<T> Final<T> {
  7. pub fn new(val: T) -> Self {
  8. Self(val)
  9. }
  10. }
  11.  
  12. impl<T> std::ops::Deref for Final<T> {
  13. type Target = T;
  14. fn deref(&self) -> &T {
  15. &self.0
  16. }
  17. }
  18. }
  19.  
  20. use final_impl::Final;
  21.  
  22. struct MyStruct {
  23. mutable_vec: Vec<u8>,
  24. immutable_vec: Final<Vec<u8>>,
  25. }
  26.  
  27. impl MyStruct {
  28. fn new() -> Self {
  29. Self {
  30. mutable_vec: vec![0, 1, 2, 3],
  31. immutable_vec: Final::new(vec![0, 1, 2, 3]),
  32. }
  33. }
  34. }
  35.  
  36. fn main() {
  37. let mut s = MyStruct::new();
  38. s.mutable_vec.push(4);
  39.  
  40. // Does not compile
  41. s.immutable_vec.push(4);
  42. }
Add Comment
Please, Sign In to add comment