aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/position.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-10-07 20:12:36 -0500
committerGitHub <noreply@github.com>2022-10-07 20:12:36 -0500
commitbc3aa9467ae1e2d0ea1727093af9b0af14965e69 (patch)
tree8db3b735daed484507129eb0683db88ddec14210 /azalea-core/src/position.rs
parent695efef66fdf1e08f0cb6d8783c085875100fa2d (diff)
downloadazalea-drasl-bc3aa9467ae1e2d0ea1727093af9b0af14965e69.tar.xz
Replace impl Read with Cursor<&[u8]> (#26)
* Start getting rid of Cursor * try to make the tests pass and fail * make the tests pass * remove unused uses * fix clippy warnings * fix potential OOM exploits * fix OOM in az-nbt * fix nbt benchmark * fix a test * start replacing it with Cursor<Vec<u8>> * wip * fix all the issues * fix all tests * fix nbt benchmark * fix warnings
Diffstat (limited to 'azalea-core/src/position.rs')
-rw-r--r--azalea-core/src/position.rs16
1 files changed, 7 insertions, 9 deletions
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs
index d47072d4..d408d817 100644
--- a/azalea-core/src/position.rs
+++ b/azalea-core/src/position.rs
@@ -1,7 +1,7 @@
use crate::ResourceLocation;
use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use std::{
- io::{Read, Write},
+ io::{Cursor, Write},
ops::{Add, Mul, Rem},
};
@@ -301,7 +301,7 @@ const Z_OFFSET: u64 = PACKED_Y_LENGTH;
const X_OFFSET: u64 = PACKED_Y_LENGTH + PACKED_Z_LENGTH;
impl McBufReadable for BlockPos {
- fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
+ fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let val = i64::read_from(buf)?;
let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32;
let y = (val << (64 - PACKED_Y_LENGTH) >> (64 - PACKED_Y_LENGTH)) as i32;
@@ -311,7 +311,7 @@ impl McBufReadable for BlockPos {
}
impl McBufReadable for GlobalPos {
- fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
+ fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
Ok(GlobalPos {
dimension: ResourceLocation::read_from(buf)?,
pos: BlockPos::read_from(buf)?,
@@ -320,7 +320,7 @@ impl McBufReadable for GlobalPos {
}
impl McBufReadable for ChunkSectionPos {
- fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
+ fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let long = i64::read_from(buf)?;
Ok(ChunkSectionPos {
x: (long >> 42) as i32,
@@ -361,8 +361,6 @@ impl McBufWritable for ChunkSectionPos {
#[cfg(test)]
mod tests {
- use std::io::Cursor;
-
use super::*;
#[test]
@@ -403,10 +401,10 @@ mod tests {
#[test]
fn test_read_blockpos_from() {
- let mut buf = Cursor::new(Vec::new());
+ let mut buf = Vec::new();
13743895338965u64.write_into(&mut buf).unwrap();
- buf.set_position(0);
- let block_pos = BlockPos::read_from(&mut buf).unwrap();
+ let buf = &mut &buf[..];
+ let block_pos = BlockPos::read_from(buf).unwrap();
assert_eq!(block_pos, BlockPos::new(49, -43, -3));
}
}