From ddf3a7981cfed5f660c77ab25d01d8577827c4f0 Mon Sep 17 00:00:00 2001 From: Thinkofname Date: Fri, 18 Mar 2016 22:24:30 +0000 Subject: [PATCH] Base implementation for worlds/blocks --- protocol/src/macros.rs | 25 +++++++++++++++++++ protocol/src/types/bit/map.rs | 4 ++-- protocol/src/types/mod.rs | 1 + protocol/src/types/nibble.rs | 45 +++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 protocol/src/macros.rs create mode 100644 protocol/src/types/nibble.rs diff --git a/protocol/src/macros.rs b/protocol/src/macros.rs new file mode 100644 index 0000000..bf2b7a2 --- /dev/null +++ b/protocol/src/macros.rs @@ -0,0 +1,25 @@ + + +#[doc(hidden)] +#[macro_export] +macro_rules! create_ids { + ($t:ty, ) => (); + ($t:ty, prev($prev:ident), $name:ident) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = $prev + 1; + ); + ($t:ty, prev($prev:ident), $name:ident, $($n:ident),+) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = $prev + 1; + create_ids!($t, prev($name), $($n),+); + ); + ($t:ty, $name:ident, $($n:ident),+) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = 0; + create_ids!($t, prev($name), $($n),+); + ); + ($t:ty, $name:ident) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = 0; + ); +} diff --git a/protocol/src/types/bit/map.rs b/protocol/src/types/bit/map.rs index 15136a9..a847bb3 100644 --- a/protocol/src/types/bit/map.rs +++ b/protocol/src/types/bit/map.rs @@ -14,7 +14,7 @@ pub struct Map { bits: Vec, - bit_size: usize, + pub bit_size: usize, length: usize, } @@ -60,7 +60,7 @@ impl Map { map } - pub fn resize(self, size: usize) -> Map { + pub fn resize(&self, size: usize) -> Map { let mut n = Map::new(self.length, size); for i in 0..self.length { n.set(i, self.get(i)); diff --git a/protocol/src/types/mod.rs b/protocol/src/types/mod.rs index 11e4d7c..682180e 100644 --- a/protocol/src/types/mod.rs +++ b/protocol/src/types/mod.rs @@ -19,3 +19,4 @@ mod metadata; pub use self::metadata::*; pub mod bit; +pub mod nibble; diff --git a/protocol/src/types/nibble.rs b/protocol/src/types/nibble.rs new file mode 100644 index 0000000..1b62d2e --- /dev/null +++ b/protocol/src/types/nibble.rs @@ -0,0 +1,45 @@ +// Copyright 2015 Matthew Collins +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +pub struct Array { + data: Vec, +} + +impl Array { + pub fn new(size: usize) -> Array { + Array { + data: vec![0; (size + 1) >> 1], + } + } + + pub fn get(&self, idx: usize) -> u8 { + let val = self.data[idx>>1]; + if idx&1 == 0 { + val & 0xF + } else { + val >> 4 + } + } + + pub fn set(&mut self, idx: usize, val: u8) { + let i = idx >> 1; + let old = self.data[i]; + if idx&1 == 0 { + self.data[i] = (old & 0xF0) | (val & 0xF); + } else { + self.data[i] = (old & 0x0F) | ((val & 0xF) << 4); + } + } +}