use crate::{read::BufReadError, McBufReadable, McBufWritable}; use std::{ io::{Read, Write}, ops::Deref, }; /// A Vec that isn't prefixed by a VarInt with the size. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct UnsizedByteArray(Vec); impl Deref for UnsizedByteArray { type Target = Vec; fn deref(&self) -> &Self::Target { &self.0 } } impl From> for UnsizedByteArray { fn from(vec: Vec) -> Self { Self(vec) } } impl From<&str> for UnsizedByteArray { fn from(s: &str) -> Self { Self(s.as_bytes().to_vec()) } } /// Represents Java's BitSet, a list of bits. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct BitSet { data: Vec, } // the Index trait requires us to return a reference, but we can't do that impl BitSet { pub fn index(&self, index: usize) -> bool { (self.data[index / 64] & (1u64 << (index % 64))) != 0 } } impl McBufReadable for BitSet { fn read_from(buf: &mut impl Read) -> Result { Ok(Self { data: Vec::::read_from(buf)?, }) } } impl McBufWritable for BitSet { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { self.data.write_into(buf) } }