mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
get rid of read_collection
This commit is contained in:
parent
fa471dd904
commit
3d0aef772a
2 changed files with 9 additions and 23 deletions
|
@ -10,6 +10,6 @@ This project was heavily inspired by PrismarineJS.
|
||||||
|
|
||||||
- Bypass all anticheats
|
- Bypass all anticheats
|
||||||
- Only support the latest Minecraft version
|
- Only support the latest Minecraft version
|
||||||
|
- Do everything a vanilla client can do
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -107,22 +107,6 @@ pub trait Readable {
|
||||||
async fn read_byte(&mut self) -> Result<u8, String>;
|
async fn read_byte(&mut self) -> Result<u8, String>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// unfortunately we can't put this in Readable since rust gets mad
|
|
||||||
pub async fn read_collection<R, T, F, Fut>(buf: &mut R, reader: F) -> Result<Vec<T>, String>
|
|
||||||
where
|
|
||||||
R: AsyncRead + std::marker::Unpin + std::marker::Send,
|
|
||||||
T: Send,
|
|
||||||
F: FnOnce(&mut R) -> Fut + Send + Copy,
|
|
||||||
Fut: Future<Output = Result<T, String>> + Send,
|
|
||||||
{
|
|
||||||
let mut v: Vec<T> = Vec::new();
|
|
||||||
let length = buf.read_varint().await?.0;
|
|
||||||
for _ in 0..length {
|
|
||||||
v.push(reader(buf).await?);
|
|
||||||
}
|
|
||||||
Ok(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<R> Readable for R
|
impl<R> Readable for R
|
||||||
where
|
where
|
||||||
|
@ -240,19 +224,21 @@ mod tests {
|
||||||
let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123]));
|
let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123]));
|
||||||
assert_eq!(buf.read_varint().await.unwrap(), (7178, 2));
|
assert_eq!(buf.read_varint().await.unwrap(), (7178, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn readutf(r: &mut BufReader<Cursor<Vec<u8>>>) -> Result<String, String> {
|
|
||||||
r.read_utf().await
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_collection() {
|
async fn test_collection() {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
buf.write_collection(vec!["a", "bc", "def"], Vec::write_utf)
|
buf.write_collection(vec!["a", "bc", "def"], Vec::write_utf)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// there's no read_collection because idk how to do it in rust
|
||||||
let mut buf = BufReader::new(Cursor::new(buf));
|
let mut buf = BufReader::new(Cursor::new(buf));
|
||||||
let result = read_collection(&mut buf, readutf).await.unwrap();
|
|
||||||
|
let mut result = Vec::new();
|
||||||
|
let length = buf.read_varint().await.unwrap().0;
|
||||||
|
for _ in 0..length {
|
||||||
|
result.push(buf.read_utf().await.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(result, vec!["a", "bc", "def"]);
|
assert_eq!(result, vec!["a", "bc", "def"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue