QuarkDoe

Rust-play-2

Dec 20th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.39 KB | None | 0 0
  1. use std::fmt::Display;
  2. use std::fmt::Formatter;
  3. use std::fmt::Result as FmtResult;
  4. use std::num::NonZeroU8;
  5.  
  6. type V = Option<NonZeroU8>;
  7.  
  8. type B4 = [[[V; 4]; 4]; 4];
  9.  
  10. type BX4 = Box<B4>;
  11.  
  12. #[inline]
  13. fn nv(v: u8) -> V {
  14.     NonZeroU8::new(v)
  15. }
  16.  
  17. struct Box4 {
  18.     bx: Option<BX4>,
  19. }
  20.  
  21. impl Box4 {
  22.     fn new() -> Self {
  23.         Box4 { bx: None }
  24.     }
  25.  
  26.     fn init_box(&mut self) {
  27.         self.bx = Some(Box::new([[[None; 4]; 4]; 4]))
  28.     }
  29.  
  30.     fn drop_box(&mut self) {
  31.         self.bx = None
  32.     }
  33.    
  34.     fn fast_get_1( &self, x: usize, y: usize, z: usize ) -> V {
  35.         self.bx.as_ref().map( |b|{b[x][y][z]} ).unwrap()
  36.     }
  37.  
  38.     fn fast_get_2( &self, x: usize, y: usize, z: usize ) -> V {
  39.         match self.bx {
  40.             None => None,
  41.             Some(ref b) => b[x][y][z],
  42.         }
  43.     }
  44.  
  45. }
  46.  
  47. impl Clone for Box4 {
  48.     fn clone(&self) -> Self {
  49.         match self.bx {
  50.             None => Box4 { bx: None },
  51.             Some(ref v) => Box4 {
  52.                 bx: Some(Box::new(*v.clone())),
  53.             },
  54.         }
  55.     }
  56. }
  57.  
  58. // impl Display for Box4 {
  59. //     fn fmt(&self, f: &mut Formatter) -> FmtResult {
  60. //         match self.bx {
  61. //             None => {
  62. //                 write!(f, "| None |");
  63. //             }
  64. //             Some(ref l1) => {
  65. //                 // write!( f, "| Some |" );
  66. //                 for l2 in l1.iter() {
  67. //                     write!(f, "{}", "[ ");
  68. //                     for l3 in l2.iter() {
  69. //                         write!(f, "{}", "[ ");
  70. //                         for v in l3.iter() {
  71. //                             write!(f, " {:?} ", v);
  72. //                         }
  73. //                         write!(f, "{}", " ] ");
  74. //                     }
  75. //                     writeln!(f, "{}", " ]");
  76. //                 }
  77. //             }
  78. //         };
  79. //         Ok(())
  80. //     }
  81. // }
  82.  
  83. fn main() {
  84.     // println!("{}", std::mem::size_of::<B4>());
  85.     // println!("{}", std::mem::size_of::<Box4>());
  86.     // let mut t = [ Box4::new(); 128 ];
  87.     let mut tt: Vec<Box4> = Vec::new();
  88.     tt.resize(4, Box4::new());
  89.  
  90.     tt[2].init_box();
  91.     {
  92.         let bx = tt[2].bx.as_mut().unwrap();
  93.         bx[0][0][1] = nv(1);
  94.         bx[0][1][0] = nv(2);
  95.         bx[1][0][0] = nv(3);
  96.     }
  97.    
  98.     let box4 = &tt[2];
  99.    
  100.     println!("{:?}", box4.fast_get_1(0,0,1));
  101.     println!("{:?}", box4.fast_get_2(0,1,0));
  102.    
  103.     // println!("{}", tt[2]);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment