#[repr(C)]pub struct Region { /* private fields */ }
Expand description
Represents a region file, with methods to access data within it.
Implementations§
Source§impl Region
impl Region
Sourcepub fn from_slice(slice: &[u8]) -> Result<&Region>
pub fn from_slice(slice: &[u8]) -> Result<&Region>
Parse this slice into a Region. This does no input validation except confirm that the 8KiB
header is there, further validation is done in Region::get_chunk
and Chunk::parse
to help prevent unnecessary memory allocation.
Note: Changing the data in the slice after calling this method will change the Region
returned by this method, so it is advised against
Sourcepub const unsafe fn from_array<const N: usize>(arr: &[u8; N]) -> &'static Region
pub const unsafe fn from_array<const N: usize>(arr: &[u8; N]) -> &'static Region
Create a Region from an array if the size is known at compile time.
§Safety
N
>= 8192- Array should contain valid bytes for a region file, though if it doesn’t, that issue
will be caught in
Region::get_chunk
andChunk::parse
§Usage
The intended usage of this method is as a const value:
const REGION: &Region = unsafe { Region::from_array(include_bytes!("../test/r.0.0.mca")) };
This method will panic if N
< 8192, thus failing to compile when used as a const value:
const REGION: &Region = unsafe { Region::from_array(&[0; 16]) };
Sourcepub fn from_reader<R>(r: &mut R) -> Result<Box<Region>>where
R: Read,
pub fn from_reader<R>(r: &mut R) -> Result<Box<Region>>where
R: Read,
A method for ease of use, effectively does the same thing as calling Read::read_to_end
and then passing that to Region::from_slice
, with the only difference being that it
returns an owned box rather than a reference.
§Usage
let mut file = File::open("./test/r.0.0.mca")?;
let region = Region::from_reader(&mut file)?;
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Validate that this Region contains all valid chunks by trying to parse every chunk.
§Important Note
- This method is obviously slow and uses a decent amount of memory. It is
recommended to assume the data is correct and validate it as you use the
Region::get_chunk
andChunk::parse
methods. - This method should only be used when you absolutely need to validate the data is
correct and can’t use the
Region::get_chunk
andChunk::parse
methods
Sourcepub const fn get_timestamp(&self, x: u32, z: u32) -> u32
pub const fn get_timestamp(&self, x: u32, z: u32) -> u32
Sourcepub fn get_chunk(&self, chunk_x: u32, chunk_z: u32) -> Result<Option<&Chunk>>
pub fn get_chunk(&self, chunk_x: u32, chunk_z: u32) -> Result<Option<&Chunk>>
Get a chunk from this Region
using relative coordinates within the region
§Return Values
Err
if data is invalidOk(None)
if the data is valid, but there is no chunk generatedOk(Some(&Chunk))
if the data is valid and the chunk exists
This will return a &Chunk
which references this Region
, if you want an owned
version, call Chunk::boxed
on the returned chunk.
§Panics
- If
x
andz
are not within0..=31
Sourcepub fn get_chunk_from_block(
&self,
block_x: u32,
block_z: u32,
) -> Result<Option<&Chunk>>
pub fn get_chunk_from_block( &self, block_x: u32, block_z: u32, ) -> Result<Option<&Chunk>>
Get a chunk from this Region
using relative block coordinates within the region
§Return Values
Err
if data is invalidOk(None)
if the data is valid, but there is no chunk generatedOk(Some(&Chunk))
if the data is valid and the chunk exists
This will return a &Chunk
which references this Region
, if you want an owned
version, call Chunk::boxed
on the returned chunk.
§Panics
- If
x
andz
are not within0..=511