fix warnings
This commit is contained in:
parent
761724c0ac
commit
a8299d75d1
46
convert_async.py
Normal file
46
convert_async.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import sys
|
||||||
|
import re
|
||||||
|
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print(f"Usage: {sys.argv[0]} <filename>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
filename = sys.argv[1]
|
||||||
|
|
||||||
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
pattern = re.compile(
|
||||||
|
r"""async\s+fn\s+ # async fn
|
||||||
|
(\w+)\s* # имя функции
|
||||||
|
\(([^)]*)\)\s* # аргументы
|
||||||
|
->\s*([^{\n]+)\s* # возвращаемый тип
|
||||||
|
\{\s*\n # открывающая фигурная скобка + новая строка
|
||||||
|
(.*?) # тело функции (ленивый захват)
|
||||||
|
^\} # закрывающая фигурная скобка на отдельной строке
|
||||||
|
""",
|
||||||
|
re.DOTALL | re.MULTILINE | re.VERBOSE,
|
||||||
|
)
|
||||||
|
|
||||||
|
def replacer(match):
|
||||||
|
name = match.group(1)
|
||||||
|
args = match.group(2)
|
||||||
|
ret = match.group(3).strip()
|
||||||
|
body = match.group(4)
|
||||||
|
|
||||||
|
# Добавляем 4 пробела в начале каждой строки тела
|
||||||
|
indented_body = "\n".join(" " + line if line.strip() else "" for line in body.splitlines())
|
||||||
|
|
||||||
|
return (
|
||||||
|
f"fn {name}({args}) -> impl std::future::Future<Output = {ret}> {{\n"
|
||||||
|
f" async {{\n"
|
||||||
|
f"{indented_body}\n"
|
||||||
|
f" }}\n"
|
||||||
|
f"}}"
|
||||||
|
)
|
||||||
|
|
||||||
|
new_content = pattern.sub(replacer, content)
|
||||||
|
|
||||||
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
|
f.write(new_content)
|
||||||
|
|
@ -76,203 +76,203 @@ pub trait DataReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarInt as u8
|
/// Read VarInt as u8
|
||||||
async fn read_u8_varint(&mut self) -> Result<u8, ProtocolError> {
|
fn read_u8_varint(&mut self) -> impl Future<Output = Result<u8, ProtocolError>> {
|
||||||
TryInto::<u8>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<u8>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarInt as u16
|
/// Read VarInt as u16
|
||||||
async fn read_u16_varint(&mut self) -> Result<u16, ProtocolError> {
|
fn read_u16_varint(&mut self) -> impl Future<Output = Result<u16, ProtocolError>> {
|
||||||
TryInto::<u16>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<u16>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarInt as u32
|
/// Read VarInt as u32
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i32::MAX
|
/// Returns error if the value is greater than i32::MAX
|
||||||
async fn read_u32_varint(&mut self) -> Result<u32, ProtocolError> {
|
fn read_u32_varint(&mut self) -> impl Future<Output = Result<u32, ProtocolError>> {
|
||||||
let val = self.read_varint().await?;
|
async {let val = self.read_varint().await?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)
|
TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarInt as usize
|
/// Read VarInt as usize
|
||||||
async fn read_usize_varint(&mut self) -> Result<usize, ProtocolError> {
|
fn read_usize_varint(&mut self) -> impl Future<Output = Result<usize, ProtocolError>> {
|
||||||
Ok(TryInto::<usize>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)?)
|
async {Ok(TryInto::<usize>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)?)}
|
||||||
}
|
}
|
||||||
/// Read VarInt as i8
|
/// Read VarInt as i8
|
||||||
async fn read_i8_varint(&mut self) -> Result<i8, ProtocolError> {
|
fn read_i8_varint(&mut self) -> impl Future<Output = Result<i8, ProtocolError>> {
|
||||||
TryInto::<i8>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<i8>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarInt as i16
|
/// Read VarInt as i16
|
||||||
async fn read_i16_varint(&mut self,) -> Result<i16, ProtocolError> {
|
fn read_i16_varint(&mut self,) -> impl Future<Output = Result<i16, ProtocolError>> {
|
||||||
TryInto::<i16>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<i16>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarInt as i32
|
/// Read VarInt as i32
|
||||||
///
|
///
|
||||||
/// *Use [read_varint](DataReader::read_varint) instead*
|
/// *Use [read_varint](DataReader::read_varint) instead*
|
||||||
async fn read_i32_varint(&mut self) -> Result<i32, ProtocolError> {
|
fn read_i32_varint(&mut self) -> impl Future<Output = Result<i32, ProtocolError>> {
|
||||||
self.read_varint().await
|
async {self.read_varint().await}
|
||||||
}
|
}
|
||||||
/// Read VarInt as isize
|
/// Read VarInt as isize
|
||||||
async fn read_isize_varint(&mut self) -> Result<isize, ProtocolError> {
|
fn read_isize_varint(&mut self) -> impl Future<Output = Result<isize, ProtocolError>> {
|
||||||
Ok(TryInto::<isize>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)?)
|
async {Ok(TryInto::<isize>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)?)}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarLong as u8
|
/// Read VarLong as u8
|
||||||
async fn read_u8_varlong(&mut self) -> Result<u8, ProtocolError> {
|
fn read_u8_varlong(&mut self) -> impl Future<Output = Result<u8, ProtocolError>> {
|
||||||
TryInto::<u8>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<u8>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as u16
|
/// Read VarLong as u16
|
||||||
async fn read_u16_varlong(&mut self) -> Result<u16, ProtocolError> {
|
fn read_u16_varlong(&mut self) -> impl Future<Output = Result<u16, ProtocolError>> {
|
||||||
TryInto::<u16>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<u16>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as u32
|
/// Read VarLong as u32
|
||||||
async fn read_u32_varlong(&mut self) -> Result<u32, ProtocolError> {
|
fn read_u32_varlong(&mut self) -> impl Future<Output = Result<u32, ProtocolError>> {
|
||||||
TryInto::<u32>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<u32>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as usize
|
/// Read VarLong as usize
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i64::MAX
|
/// Returns error if the value is greater than i64::MAX
|
||||||
async fn read_usize_varlong(&mut self) -> Result<usize, ProtocolError> {
|
fn read_usize_varlong(&mut self) -> impl Future<Output = Result<usize, ProtocolError>> {
|
||||||
let val = TryInto::<usize>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)?;
|
async {let val = TryInto::<usize>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)?;
|
||||||
if val as u64 > i64::MAX as u64 { return Err(ProtocolError::VarIntError); }
|
if val as u64 > i64::MAX as u64 { return Err(ProtocolError::VarIntError); }
|
||||||
Ok(val)
|
Ok(val) }
|
||||||
}
|
}
|
||||||
/// Read VarLong as u64
|
/// Read VarLong as u64
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i64::MAX
|
/// Returns error if the value is greater than i64::MAX
|
||||||
async fn read_u64_varlong(&mut self) -> Result<u64, ProtocolError> {
|
fn read_u64_varlong(&mut self) -> impl Future<Output = Result<u64, ProtocolError>> {
|
||||||
let val = self.read_varlong().await?;
|
async {let val = self.read_varlong().await?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
TryInto::<u64>::try_into(val).map_err(|_| ProtocolError::VarIntError)
|
TryInto::<u64>::try_into(val).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i8
|
/// Read VarLong as i8
|
||||||
async fn read_i8_varlong(&mut self) -> Result<i8, ProtocolError> {
|
fn read_i8_varlong(&mut self) -> impl Future<Output = Result<i8, ProtocolError>> {
|
||||||
TryInto::<i8>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<i8>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i16
|
/// Read VarLong as i16
|
||||||
async fn read_i16_varlong(&mut self) -> Result<i16, ProtocolError> {
|
fn read_i16_varlong(&mut self) -> impl Future<Output = Result<i16, ProtocolError>> {
|
||||||
TryInto::<i16>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<i16>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i32
|
/// Read VarLong as i32
|
||||||
async fn read_i32_varlong(&mut self) -> Result<i32, ProtocolError> {
|
fn read_i32_varlong(&mut self) -> impl Future<Output = Result<i32, ProtocolError>> {
|
||||||
TryInto::<i32>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
async {TryInto::<i32>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as isize
|
/// Read VarLong as isize
|
||||||
async fn read_isize_varlong(&mut self) -> Result<isize, ProtocolError> {
|
fn read_isize_varlong(&mut self) -> impl Future<Output = Result<isize, ProtocolError>> {
|
||||||
Ok(TryInto::<isize>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)?)
|
async {Ok(TryInto::<isize>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)?)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i64
|
/// Read VarLong as i64
|
||||||
///
|
///
|
||||||
/// *Use [read_varlong](DataReader::read_varlong) instead*
|
/// *Use [read_varlong](DataReader::read_varlong) instead*
|
||||||
async fn read_i64_varlong(&mut self) -> Result<i64, ProtocolError> {
|
fn read_i64_varlong(&mut self) -> impl Future<Output = Result<i64, ProtocolError>> {
|
||||||
self.read_varlong().await
|
async {self.read_varlong().await}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarInt as u8 with size in bytes (value, size)
|
/// Read VarInt as u8 with size in bytes (value, size)
|
||||||
async fn read_u8_varint_size(&mut self) -> Result<(u8, usize), ProtocolError> {
|
fn read_u8_varint_size(&mut self) -> impl Future<Output = Result<(u8, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varint_size().await?;
|
async {let (val, size) = self.read_varint_size().await?;
|
||||||
Ok((TryInto::<u8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<u8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarInt as u16 with size in bytes (value, size)
|
/// Read VarInt as u16 with size in bytes (value, size)
|
||||||
async fn read_u16_varint_size(&mut self) -> Result<(u16, usize), ProtocolError> {
|
fn read_u16_varint_size(&mut self) -> impl Future<Output = Result<(u16, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varint_size().await?;
|
async {let (val, size) = self.read_varint_size().await?;
|
||||||
Ok((TryInto::<u16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<u16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarInt as u32 with size in bytes (value, size)
|
/// Read VarInt as u32 with size in bytes (value, size)
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i32::MAX
|
/// Returns error if the value is greater than i32::MAX
|
||||||
async fn read_u32_varint_size(&mut self) -> Result<(u32, usize), ProtocolError> {
|
fn read_u32_varint_size(&mut self) -> impl Future<Output = Result<(u32, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varint_size().await?;
|
async {let (val, size) = self.read_varint_size().await?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
Ok((TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarInt as usize with size in bytes (value, size)
|
/// Read VarInt as usize with size in bytes (value, size)
|
||||||
async fn read_usize_varint_size(&mut self) -> Result<(usize, usize), ProtocolError> {
|
fn read_usize_varint_size(&mut self) -> impl Future<Output = Result<(usize, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varint_size().await?;
|
async {let (val, size) = self.read_varint_size().await?;
|
||||||
Ok((TryInto::<usize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<usize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarInt as i8 with size in bytes (value, size)
|
/// Read VarInt as i8 with size in bytes (value, size)
|
||||||
async fn read_i8_varint_size(&mut self) -> Result<(i8, usize), ProtocolError> {
|
fn read_i8_varint_size(&mut self) -> impl Future<Output = Result<(i8, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varint_size().await?;
|
async {let (val, size) = self.read_varint_size().await?;
|
||||||
Ok((TryInto::<i8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<i8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarInt as i16 with size in bytes (value, size)
|
/// Read VarInt as i16 with size in bytes (value, size)
|
||||||
async fn read_i16_varint_size(&mut self,) -> Result<(i16, usize), ProtocolError> {
|
fn read_i16_varint_size(&mut self,) -> impl Future<Output = Result<(i16, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varint_size().await?;
|
async {let (val, size) = self.read_varint_size().await?;
|
||||||
Ok((TryInto::<i16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<i16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarInt as i32 with size in bytes (value, size)
|
/// Read VarInt as i32 with size in bytes (value, size)
|
||||||
///
|
///
|
||||||
/// *Use [read_varint_size](DataReader::read_varint_size) instead*
|
/// *Use [read_varint_size](DataReader::read_varint_size) instead*
|
||||||
async fn read_i32_varint_size(&mut self) -> Result<(i32, usize), ProtocolError> {
|
fn read_i32_varint_size(&mut self) -> impl Future<Output = Result<(i32, usize), ProtocolError>> {
|
||||||
self.read_varint_size().await
|
async {self.read_varint_size().await}
|
||||||
}
|
}
|
||||||
/// Read VarInt as isize with size in bytes (value, size)
|
/// Read VarInt as isize with size in bytes (value, size)
|
||||||
async fn read_isize_varint_size(&mut self) -> Result<(isize, usize), ProtocolError> {
|
fn read_isize_varint_size(&mut self) -> impl Future<Output = Result<(isize, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varint_size().await?;
|
async {let (val, size) = self.read_varint_size().await?;
|
||||||
Ok((TryInto::<isize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<isize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarLong as u8 with size in bytes (value, size)
|
/// Read VarLong as u8 with size in bytes (value, size)
|
||||||
async fn read_u8_varlong_size(&mut self) -> Result<(u8, usize), ProtocolError> {
|
fn read_u8_varlong_size(&mut self) -> impl Future<Output = Result<(u8, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
Ok((TryInto::<u8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<u8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as u16 with size in bytes (value, size)
|
/// Read VarLong as u16 with size in bytes (value, size)
|
||||||
async fn read_u16_varlong_size(&mut self) -> Result<(u16, usize), ProtocolError> {
|
fn read_u16_varlong_size(&mut self) -> impl Future<Output = Result<(u16, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
Ok((TryInto::<u16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<u16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as u32 with size in bytes (value, size)
|
/// Read VarLong as u32 with size in bytes (value, size)
|
||||||
async fn read_u32_varlong_size(&mut self) -> Result<(u32, usize), ProtocolError> {
|
fn read_u32_varlong_size(&mut self) -> impl Future<Output = Result<(u32, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
Ok((TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as usize with size in bytes (value, size)
|
/// Read VarLong as usize with size in bytes (value, size)
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i64::MAX
|
/// Returns error if the value is greater than i64::MAX
|
||||||
async fn read_usize_varlong_size(&mut self) -> Result<(usize, usize), ProtocolError> {
|
fn read_usize_varlong_size(&mut self) -> impl Future<Output = Result<(usize, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
let val = TryInto::<usize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?;
|
let val = TryInto::<usize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?;
|
||||||
if val as u64 > i64::MAX as u64 { return Err(ProtocolError::VarIntError); }
|
if val as u64 > i64::MAX as u64 { return Err(ProtocolError::VarIntError); }
|
||||||
Ok((val, size))
|
Ok((val, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as u64 with size in bytes (value, size)
|
/// Read VarLong as u64 with size in bytes (value, size)
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i64::MAX
|
/// Returns error if the value is greater than i64::MAX
|
||||||
async fn read_u64_varlong_size(&mut self) -> Result<(u64, usize), ProtocolError> {
|
fn read_u64_varlong_size(&mut self) -> impl Future<Output = Result<(u64, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
Ok((TryInto::<u64>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<u64>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i8 with size in bytes (value, size)
|
/// Read VarLong as i8 with size in bytes (value, size)
|
||||||
async fn read_i8_varlong_size(&mut self) -> Result<(i8, usize), ProtocolError> {
|
fn read_i8_varlong_size(&mut self) -> impl Future<Output = Result<(i8, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
Ok((TryInto::<i8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<i8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i16 with size in bytes (value, size)
|
/// Read VarLong as i16 with size in bytes (value, size)
|
||||||
async fn read_i16_varlong_size(&mut self) -> Result<(i16, usize), ProtocolError> {
|
fn read_i16_varlong_size(&mut self) -> impl Future<Output = Result<(i16, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
Ok((TryInto::<i16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<i16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i32 with size in bytes (value, size)
|
/// Read VarLong as i32 with size in bytes (value, size)
|
||||||
async fn read_i32_varlong_size(&mut self) -> Result<(i32, usize), ProtocolError> {
|
fn read_i32_varlong_size(&mut self) -> impl Future<Output = Result<(i32, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
Ok((TryInto::<i32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<i32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as isize with size in bytes (value, size)
|
/// Read VarLong as isize with size in bytes (value, size)
|
||||||
async fn read_isize_varlong_size(&mut self) -> Result<(isize, usize), ProtocolError> {
|
fn read_isize_varlong_size(&mut self) -> impl Future<Output = Result<(isize, usize), ProtocolError>> {
|
||||||
let (val, size) = self.read_varlong_size().await?;
|
async {let (val, size) = self.read_varlong_size().await?;
|
||||||
Ok((TryInto::<isize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
Ok((TryInto::<isize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i64 with size in bytes (value, size)
|
/// Read VarLong as i64 with size in bytes (value, size)
|
||||||
///
|
///
|
||||||
/// *Use [read_varlong_size](DataReader::read_varlong_size) instead*
|
/// *Use [read_varlong_size](DataReader::read_varlong_size) instead*
|
||||||
async fn read_i64_varlong_size(&mut self) -> Result<(i64, usize), ProtocolError> {
|
fn read_i64_varlong_size(&mut self) -> impl Future<Output = Result<(i64, usize), ProtocolError>> {
|
||||||
self.read_varlong_size().await
|
async {self.read_varlong_size().await}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarInt as i32 with size in bytes (value, size)
|
/// Read VarInt as i32 with size in bytes (value, size)
|
||||||
async fn read_varint_size(&mut self) -> Result<(i32, usize), ProtocolError> {
|
fn read_varint_size(&mut self) -> impl Future<Output = Result<(i32, usize), ProtocolError>> {
|
||||||
let mut value: u32 = 0;
|
async {let mut value: u32 = 0;
|
||||||
let mut position: u32 = 0;
|
let mut position: u32 = 0;
|
||||||
let mut size = 0;
|
let mut size = 0;
|
||||||
|
|
||||||
@ -289,11 +289,11 @@ pub trait DataReader {
|
|||||||
position += 7;
|
position += 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((value as i32, size))
|
Ok((value as i32, size))}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i64 with size in bytes (value, size)
|
/// Read VarLong as i64 with size in bytes (value, size)
|
||||||
async fn read_varlong_size(&mut self) -> Result<(i64, usize), ProtocolError> {
|
fn read_varlong_size(&mut self) -> impl Future<Output = Result<(i64, usize), ProtocolError>> {
|
||||||
let mut value: u64 = 0;
|
async {let mut value: u64 = 0;
|
||||||
let mut position: u32 = 0;
|
let mut position: u32 = 0;
|
||||||
let mut size = 0;
|
let mut size = 0;
|
||||||
|
|
||||||
@ -310,16 +310,16 @@ pub trait DataReader {
|
|||||||
position += 7;
|
position += 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((value as i64, size))
|
Ok((value as i64, size))}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read VarInt as i32
|
/// Read VarInt as i32
|
||||||
async fn read_varint(&mut self) -> Result<i32, ProtocolError> {
|
fn read_varint(&mut self) -> impl Future<Output = Result<i32, ProtocolError>> {
|
||||||
self.read_varint_size().await.map(|o| o.0)
|
async {self.read_varint_size().await.map(|o| o.0)}
|
||||||
}
|
}
|
||||||
/// Read VarLong as i64
|
/// Read VarLong as i64
|
||||||
async fn read_varlong(&mut self) -> Result<i64, ProtocolError> {
|
fn read_varlong(&mut self) -> impl Future<Output = Result<i64, ProtocolError>> {
|
||||||
self.read_varlong_size().await.map(|o| o.0)
|
async {self.read_varlong_size().await.map(|o| o.0)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
347
src/data/reader.rs.prev
Normal file
347
src/data/reader.rs.prev
Normal file
@ -0,0 +1,347 @@
|
|||||||
|
use crate::ProtocolError;
|
||||||
|
use tokio::io::AsyncReadExt;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
/// Packet data reader trait
|
||||||
|
pub trait DataReader {
|
||||||
|
/// Read bytes
|
||||||
|
fn read_bytes(&mut self, size: usize) -> impl Future<Output = Result<Vec<u8>, ProtocolError>>;
|
||||||
|
|
||||||
|
/// Read byte
|
||||||
|
fn read_byte(&mut self) -> impl Future<Output = Result<u8, ProtocolError>> {
|
||||||
|
async {Ok(self.read_bytes(1).await?[0])}
|
||||||
|
}
|
||||||
|
/// Read String
|
||||||
|
fn read_string(&mut self) -> impl Future<Output = Result<String, ProtocolError>> {
|
||||||
|
async {
|
||||||
|
let size = self.read_usize_varint().await?;
|
||||||
|
match String::from_utf8(self.read_bytes(size).await?) {
|
||||||
|
Ok(i) => Ok(i),
|
||||||
|
Err(_) => Err(ProtocolError::StringParseError),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Read Unsigned Short as u16
|
||||||
|
fn read_unsigned_short(&mut self) -> impl Future<Output = Result<u16, ProtocolError>> {
|
||||||
|
async {match self.read_bytes(2).await?.try_into() {
|
||||||
|
Ok(i) => Ok(u16::from_be_bytes(i)),
|
||||||
|
Err(_) => Err(ProtocolError::ReadError),
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
/// Read Boolean
|
||||||
|
fn read_boolean(&mut self) -> impl Future<Output = Result<bool, ProtocolError>> {
|
||||||
|
async {Ok(self.read_byte().await? == 0x01)}
|
||||||
|
}
|
||||||
|
/// Read Short as i16
|
||||||
|
fn read_short(&mut self) -> impl Future<Output = Result<i16, ProtocolError>> {
|
||||||
|
async {match self.read_bytes(2).await?.try_into() {
|
||||||
|
Ok(i) => Ok(i16::from_be_bytes(i)),
|
||||||
|
Err(_) => Err(ProtocolError::ReadError),
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
/// Read Long as i64
|
||||||
|
fn read_long(&mut self) -> impl Future<Output = Result<i64, ProtocolError>> {
|
||||||
|
async {match self.read_bytes(8).await?.try_into() {
|
||||||
|
Ok(i) => Ok(i64::from_be_bytes(i)),
|
||||||
|
Err(_) => Err(ProtocolError::ReadError),
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
/// Read Float as f32
|
||||||
|
fn read_float(&mut self) -> impl Future<Output = Result<f32, ProtocolError>> {
|
||||||
|
async {match self.read_bytes(4).await?.try_into() {
|
||||||
|
Ok(i) => Ok(f32::from_be_bytes(i)),
|
||||||
|
Err(_) => Err(ProtocolError::ReadError),
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
/// Read Double as f64
|
||||||
|
fn read_double(&mut self) -> impl Future<Output = Result<f64, ProtocolError>> {
|
||||||
|
async {match self.read_bytes(8).await?.try_into() {
|
||||||
|
Ok(i) => Ok(f64::from_be_bytes(i)),
|
||||||
|
Err(_) => Err(ProtocolError::ReadError),
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
/// Read Int as i32
|
||||||
|
fn read_int(&mut self) -> impl Future<Output = Result<i32, ProtocolError>> {
|
||||||
|
async {match self.read_bytes(4).await?.try_into() {
|
||||||
|
Ok(i) => Ok(i32::from_be_bytes(i)),
|
||||||
|
Err(_) => Err(ProtocolError::ReadError),
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
/// Read UUID
|
||||||
|
fn read_uuid(&mut self) -> impl Future<Output = Result<Uuid, ProtocolError>> {
|
||||||
|
async {match self.read_bytes(16).await?.try_into() {
|
||||||
|
Ok(i) => Ok(Uuid::from_bytes(i)),
|
||||||
|
Err(_) => Err(ProtocolError::ReadError),
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read VarInt as u8
|
||||||
|
async fn read_u8_varint(&mut self) -> Result<u8, ProtocolError> {
|
||||||
|
TryInto::<u8>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarInt as u16
|
||||||
|
async fn read_u16_varint(&mut self) -> Result<u16, ProtocolError> {
|
||||||
|
TryInto::<u16>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read VarInt as u32
|
||||||
|
///
|
||||||
|
/// Returns error if the value is greater than i32::MAX
|
||||||
|
async fn read_u32_varint(&mut self) -> Result<u32, ProtocolError> {
|
||||||
|
let val = self.read_varint().await?;
|
||||||
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
|
TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarInt as usize
|
||||||
|
async fn read_usize_varint(&mut self) -> Result<usize, ProtocolError> {
|
||||||
|
Ok(TryInto::<usize>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)?)
|
||||||
|
}
|
||||||
|
/// Read VarInt as i8
|
||||||
|
async fn read_i8_varint(&mut self) -> Result<i8, ProtocolError> {
|
||||||
|
TryInto::<i8>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarInt as i16
|
||||||
|
async fn read_i16_varint(&mut self,) -> Result<i16, ProtocolError> {
|
||||||
|
TryInto::<i16>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarInt as i32
|
||||||
|
///
|
||||||
|
/// *Use [read_varint](DataReader::read_varint) instead*
|
||||||
|
async fn read_i32_varint(&mut self) -> Result<i32, ProtocolError> {
|
||||||
|
self.read_varint().await
|
||||||
|
}
|
||||||
|
/// Read VarInt as isize
|
||||||
|
async fn read_isize_varint(&mut self) -> Result<isize, ProtocolError> {
|
||||||
|
Ok(TryInto::<isize>::try_into(self.read_varint().await?).map_err(|_| ProtocolError::VarIntError)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read VarLong as u8
|
||||||
|
async fn read_u8_varlong(&mut self) -> Result<u8, ProtocolError> {
|
||||||
|
TryInto::<u8>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarLong as u16
|
||||||
|
async fn read_u16_varlong(&mut self) -> Result<u16, ProtocolError> {
|
||||||
|
TryInto::<u16>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarLong as u32
|
||||||
|
async fn read_u32_varlong(&mut self) -> Result<u32, ProtocolError> {
|
||||||
|
TryInto::<u32>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarLong as usize
|
||||||
|
///
|
||||||
|
/// Returns error if the value is greater than i64::MAX
|
||||||
|
async fn read_usize_varlong(&mut self) -> Result<usize, ProtocolError> {
|
||||||
|
let val = TryInto::<usize>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)?;
|
||||||
|
if val as u64 > i64::MAX as u64 { return Err(ProtocolError::VarIntError); }
|
||||||
|
Ok(val)
|
||||||
|
}
|
||||||
|
/// Read VarLong as u64
|
||||||
|
///
|
||||||
|
/// Returns error if the value is greater than i64::MAX
|
||||||
|
async fn read_u64_varlong(&mut self) -> Result<u64, ProtocolError> {
|
||||||
|
let val = self.read_varlong().await?;
|
||||||
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
|
TryInto::<u64>::try_into(val).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarLong as i8
|
||||||
|
async fn read_i8_varlong(&mut self) -> Result<i8, ProtocolError> {
|
||||||
|
TryInto::<i8>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarLong as i16
|
||||||
|
async fn read_i16_varlong(&mut self) -> Result<i16, ProtocolError> {
|
||||||
|
TryInto::<i16>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarLong as i32
|
||||||
|
async fn read_i32_varlong(&mut self) -> Result<i32, ProtocolError> {
|
||||||
|
TryInto::<i32>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)
|
||||||
|
}
|
||||||
|
/// Read VarLong as isize
|
||||||
|
async fn read_isize_varlong(&mut self) -> Result<isize, ProtocolError> {
|
||||||
|
Ok(TryInto::<isize>::try_into(self.read_varlong().await?).map_err(|_| ProtocolError::VarIntError)?)
|
||||||
|
}
|
||||||
|
/// Read VarLong as i64
|
||||||
|
///
|
||||||
|
/// *Use [read_varlong](DataReader::read_varlong) instead*
|
||||||
|
async fn read_i64_varlong(&mut self) -> Result<i64, ProtocolError> {
|
||||||
|
self.read_varlong().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read VarInt as u8 with size in bytes (value, size)
|
||||||
|
async fn read_u8_varint_size(&mut self) -> Result<(u8, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varint_size().await?;
|
||||||
|
Ok((TryInto::<u8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarInt as u16 with size in bytes (value, size)
|
||||||
|
async fn read_u16_varint_size(&mut self) -> Result<(u16, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varint_size().await?;
|
||||||
|
Ok((TryInto::<u16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarInt as u32 with size in bytes (value, size)
|
||||||
|
///
|
||||||
|
/// Returns error if the value is greater than i32::MAX
|
||||||
|
async fn read_u32_varint_size(&mut self) -> Result<(u32, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varint_size().await?;
|
||||||
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
|
Ok((TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarInt as usize with size in bytes (value, size)
|
||||||
|
async fn read_usize_varint_size(&mut self) -> Result<(usize, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varint_size().await?;
|
||||||
|
Ok((TryInto::<usize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarInt as i8 with size in bytes (value, size)
|
||||||
|
async fn read_i8_varint_size(&mut self) -> Result<(i8, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varint_size().await?;
|
||||||
|
Ok((TryInto::<i8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarInt as i16 with size in bytes (value, size)
|
||||||
|
async fn read_i16_varint_size(&mut self,) -> Result<(i16, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varint_size().await?;
|
||||||
|
Ok((TryInto::<i16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarInt as i32 with size in bytes (value, size)
|
||||||
|
///
|
||||||
|
/// *Use [read_varint_size](DataReader::read_varint_size) instead*
|
||||||
|
async fn read_i32_varint_size(&mut self) -> Result<(i32, usize), ProtocolError> {
|
||||||
|
self.read_varint_size().await
|
||||||
|
}
|
||||||
|
/// Read VarInt as isize with size in bytes (value, size)
|
||||||
|
async fn read_isize_varint_size(&mut self) -> Result<(isize, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varint_size().await?;
|
||||||
|
Ok((TryInto::<isize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read VarLong as u8 with size in bytes (value, size)
|
||||||
|
async fn read_u8_varlong_size(&mut self) -> Result<(u8, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
Ok((TryInto::<u8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as u16 with size in bytes (value, size)
|
||||||
|
async fn read_u16_varlong_size(&mut self) -> Result<(u16, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
Ok((TryInto::<u16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as u32 with size in bytes (value, size)
|
||||||
|
async fn read_u32_varlong_size(&mut self) -> Result<(u32, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
Ok((TryInto::<u32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as usize with size in bytes (value, size)
|
||||||
|
///
|
||||||
|
/// Returns error if the value is greater than i64::MAX
|
||||||
|
async fn read_usize_varlong_size(&mut self) -> Result<(usize, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
let val = TryInto::<usize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?;
|
||||||
|
if val as u64 > i64::MAX as u64 { return Err(ProtocolError::VarIntError); }
|
||||||
|
Ok((val, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as u64 with size in bytes (value, size)
|
||||||
|
///
|
||||||
|
/// Returns error if the value is greater than i64::MAX
|
||||||
|
async fn read_u64_varlong_size(&mut self) -> Result<(u64, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
|
Ok((TryInto::<u64>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as i8 with size in bytes (value, size)
|
||||||
|
async fn read_i8_varlong_size(&mut self) -> Result<(i8, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
Ok((TryInto::<i8>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as i16 with size in bytes (value, size)
|
||||||
|
async fn read_i16_varlong_size(&mut self) -> Result<(i16, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
Ok((TryInto::<i16>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as i32 with size in bytes (value, size)
|
||||||
|
async fn read_i32_varlong_size(&mut self) -> Result<(i32, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
Ok((TryInto::<i32>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as isize with size in bytes (value, size)
|
||||||
|
async fn read_isize_varlong_size(&mut self) -> Result<(isize, usize), ProtocolError> {
|
||||||
|
let (val, size) = self.read_varlong_size().await?;
|
||||||
|
Ok((TryInto::<isize>::try_into(val).map_err(|_| ProtocolError::VarIntError)?, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as i64 with size in bytes (value, size)
|
||||||
|
///
|
||||||
|
/// *Use [read_varlong_size](DataReader::read_varlong_size) instead*
|
||||||
|
async fn read_i64_varlong_size(&mut self) -> Result<(i64, usize), ProtocolError> {
|
||||||
|
self.read_varlong_size().await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read VarInt as i32 with size in bytes (value, size)
|
||||||
|
async fn read_varint_size(&mut self) -> Result<(i32, usize), ProtocolError> {
|
||||||
|
let mut value: u32 = 0;
|
||||||
|
let mut position: u32 = 0;
|
||||||
|
let mut size = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let byte = self.read_byte().await?;
|
||||||
|
value |= TryInto::<u32>::try_into(byte & 0x7F).map_err(|_| ProtocolError::VarIntError)? << position;
|
||||||
|
|
||||||
|
size += 1;
|
||||||
|
|
||||||
|
if (byte & 0x80) == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
position += 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((value as i32, size))
|
||||||
|
}
|
||||||
|
/// Read VarLong as i64 with size in bytes (value, size)
|
||||||
|
async fn read_varlong_size(&mut self) -> Result<(i64, usize), ProtocolError> {
|
||||||
|
let mut value: u64 = 0;
|
||||||
|
let mut position: u32 = 0;
|
||||||
|
let mut size = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let byte = self.read_byte().await?;
|
||||||
|
value |= TryInto::<u64>::try_into(byte & 0x7F).map_err(|_| ProtocolError::VarIntError)? << position;
|
||||||
|
|
||||||
|
size += 1;
|
||||||
|
|
||||||
|
if (byte & 0x80) == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
position += 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((value as i64, size))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read VarInt as i32
|
||||||
|
async fn read_varint(&mut self) -> Result<i32, ProtocolError> {
|
||||||
|
self.read_varint_size().await.map(|o| o.0)
|
||||||
|
}
|
||||||
|
/// Read VarLong as i64
|
||||||
|
async fn read_varlong(&mut self) -> Result<i64, ProtocolError> {
|
||||||
|
self.read_varlong_size().await.map(|o| o.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: AsyncReadExt + Unpin> DataReader for R {
|
||||||
|
async fn read_bytes(&mut self, size: usize) -> Result<Vec<u8>, ProtocolError> {
|
||||||
|
let mut data = Vec::new();
|
||||||
|
|
||||||
|
while data.len() < size {
|
||||||
|
let mut buf = vec![0; size - data.len()];
|
||||||
|
|
||||||
|
let n = self.read(&mut buf).await
|
||||||
|
.map_err(|_| ProtocolError::ReadError)?;
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
return Err(ProtocolError::ConnectionClosedError);
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.truncate(n);
|
||||||
|
|
||||||
|
data.append(&mut buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
}
|
@ -53,98 +53,98 @@ pub trait DataWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Write VarInt as u8
|
/// Write VarInt as u8
|
||||||
async fn write_u8_varint(&mut self, val: u8) -> Result<(), ProtocolError> {
|
fn write_u8_varint(&mut self, val: u8) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarInt as u16
|
/// Write VarInt as u16
|
||||||
async fn write_u16_varint(&mut self, val: u16) -> Result<(), ProtocolError> {
|
fn write_u16_varint(&mut self, val: u16) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarInt as usize
|
/// Write VarInt as usize
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i32::MAX
|
/// Returns error if the value is greater than i32::MAX
|
||||||
async fn write_usize_varint(&mut self, val: usize) -> Result<(), ProtocolError> {
|
fn write_usize_varint(&mut self, val: usize) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
async move { let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
self.write_varint(val).await
|
self.write_varint(val).await }
|
||||||
}
|
}
|
||||||
/// Write VarInt as u32
|
/// Write VarInt as u32
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i32::MAX
|
/// Returns error if the value is greater than i32::MAX
|
||||||
async fn write_u32_varint(&mut self, val: u32) -> Result<(), ProtocolError> {
|
fn write_u32_varint(&mut self, val: u32) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
async move { let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
self.write_varint(val).await
|
self.write_varint(val).await }
|
||||||
}
|
}
|
||||||
/// Write VarInt as i8
|
/// Write VarInt as i8
|
||||||
async fn write_i8_varint(&mut self, val: i8) -> Result<(), ProtocolError> {
|
fn write_i8_varint(&mut self, val: i8) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarInt as i16
|
/// Write VarInt as i16
|
||||||
async fn write_i16_varint(&mut self, val: i16) -> Result<(), ProtocolError> {
|
fn write_i16_varint(&mut self, val: i16) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarInt as isize
|
/// Write VarInt as isize
|
||||||
async fn write_isize_varint(&mut self, val: isize) -> Result<(), ProtocolError> {
|
fn write_isize_varint(&mut self, val: isize) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varint(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarInt as i32
|
/// Write VarInt as i32
|
||||||
///
|
///
|
||||||
/// *Use [write_varint](DataWriter::write_varint) instead*
|
/// *Use [write_varint](DataWriter::write_varint) instead*
|
||||||
async fn write_i32_varint(&mut self, val: i32) -> Result<(), ProtocolError> {
|
fn write_i32_varint(&mut self, val: i32) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varint(val).await
|
async move { self.write_varint(val).await }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write VarLong as u8
|
/// Write VarLong as u8
|
||||||
async fn write_u8_varlong(&mut self, val: u8) -> Result<(), ProtocolError> {
|
fn write_u8_varlong(&mut self, val: u8) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as u16
|
/// Write VarLong as u16
|
||||||
async fn write_u16_varlong(&mut self, val: u16) -> Result<(), ProtocolError> {
|
fn write_u16_varlong(&mut self, val: u16) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as u32
|
/// Write VarLong as u32
|
||||||
async fn write_u32_varlong(&mut self, val: u32) -> Result<(), ProtocolError> {
|
fn write_u32_varlong(&mut self, val: u32) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as u64
|
/// Write VarLong as u64
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i64::MAX
|
/// Returns error if the value is greater than i64::MAX
|
||||||
async fn write_u64_varlong(&mut self, val: u64) -> Result<(), ProtocolError> {
|
fn write_u64_varlong(&mut self, val: u64) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
async move { let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
self.write_varlong(val).await
|
self.write_varlong(val).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as usize
|
/// Write VarLong as usize
|
||||||
///
|
///
|
||||||
/// Returns error if the value is greater than i64::MAX
|
/// Returns error if the value is greater than i64::MAX
|
||||||
async fn write_usize_varlong(&mut self, val: u64) -> Result<(), ProtocolError> {
|
fn write_usize_varlong(&mut self, val: u64) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
async move { let val = val.try_into().map_err(|_| ProtocolError::VarIntError)?;
|
||||||
if val < 0 { return Err(ProtocolError::VarIntError); }
|
if val < 0 { return Err(ProtocolError::VarIntError); }
|
||||||
self.write_varlong(val).await
|
self.write_varlong(val).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as i8
|
/// Write VarLong as i8
|
||||||
async fn write_i8_varlong(&mut self, val: i8) -> Result<(), ProtocolError> {
|
fn write_i8_varlong(&mut self, val: i8) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as i16
|
/// Write VarLong as i16
|
||||||
async fn write_i16_varlong(&mut self, val: i16) -> Result<(), ProtocolError> {
|
fn write_i16_varlong(&mut self, val: i16) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as i32
|
/// Write VarLong as i32
|
||||||
async fn write_i32_varlong(&mut self, val: i32) -> Result<(), ProtocolError> {
|
fn write_i32_varlong(&mut self, val: i32) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await
|
async move { self.write_varlong(val.try_into().map_err(|_| ProtocolError::VarIntError)?).await }
|
||||||
}
|
}
|
||||||
/// Write VarLong as i64
|
/// Write VarLong as i64
|
||||||
///
|
///
|
||||||
/// *Use [write_varlong](DataWriter::write_varlong) instead*
|
/// *Use [write_varlong](DataWriter::write_varlong) instead*
|
||||||
async fn write_i64_varlong(&mut self, val: i64) -> Result<(), ProtocolError> {
|
fn write_i64_varlong(&mut self, val: i64) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
self.write_varlong(val).await
|
async move { self.write_varlong(val).await }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write VarInt as i32
|
/// Write VarInt as i32
|
||||||
async fn write_varint(&mut self, val: i32) -> Result<(), ProtocolError> {
|
fn write_varint(&mut self, val: i32) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
let mut value = val as u32;
|
async move { let mut value = val as u32;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if value & !0x7F == 0 {
|
if value & !0x7F == 0 {
|
||||||
@ -157,11 +157,11 @@ pub trait DataWriter {
|
|||||||
value >>= 7;
|
value >>= 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(()) }
|
||||||
}
|
}
|
||||||
/// Write VarLong as i64
|
/// Write VarLong as i64
|
||||||
async fn write_varlong(&mut self, val: i64) -> Result<(), ProtocolError> {
|
fn write_varlong(&mut self, val: i64) -> impl Future<Output = Result<(), ProtocolError>> {
|
||||||
let mut value = val as u64;
|
async move { let mut value = val as u64;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if value & !0x7F == 0 {
|
if value & !0x7F == 0 {
|
||||||
@ -174,7 +174,7 @@ pub trait DataWriter {
|
|||||||
value >>= 7;
|
value >>= 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(()) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user