diff options
| author | mat <github@matdoes.dev> | 2021-12-16 14:25:02 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-16 14:25:02 -0600 |
| commit | 3d0aef772a10cde37a58b18ba1409e99a5cb581a (patch) | |
| tree | 1de11c0d2985bddf6f27543d1b5f37bd862770a6 /azalea-protocol | |
| parent | fa471dd90484136230e2b7315f2d5ed5b3e5a0c8 (diff) | |
| download | azalea-drasl-3d0aef772a10cde37a58b18ba1409e99a5cb581a.tar.xz | |
get rid of read_collection
Diffstat (limited to 'azalea-protocol')
| -rw-r--r-- | azalea-protocol/src/mc_buf.rs | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/azalea-protocol/src/mc_buf.rs b/azalea-protocol/src/mc_buf.rs index 6eab5db3..e165eba0 100644 --- a/azalea-protocol/src/mc_buf.rs +++ b/azalea-protocol/src/mc_buf.rs @@ -107,22 +107,6 @@ pub trait Readable { 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] impl<R> Readable for R where @@ -240,19 +224,21 @@ mod tests { let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123])); 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] async fn test_collection() { let mut buf = Vec::new(); buf.write_collection(vec!["a", "bc", "def"], Vec::write_utf) .unwrap(); + // there's no read_collection because idk how to do it in rust 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"]); } } |
