1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use binary_utils::{Result, write_bytes, ListDataReader, Error, read_byte};
use tokio::io::{AsyncWrite, AsyncRead, AsyncReadExt as _, AsyncWriteExt as _};
use crate::ImportantFunctions;
use super::*;

impl DataWriter for UUID {
    async fn write(&self, writer: &mut (impl AsyncWrite + Unpin)) -> Result<()> {
        write_bytes(writer, &self.0.to_be_bytes()).await?;
        Ok(())
    }
}
impl DataReader for UUID {
    async fn read(reader: &mut (impl AsyncRead + Unpin)) -> Result<Self> {
        use crate::Long;
        let l0 = Long::read(reader).await?.get_value();
        let l1 = Long::read(reader).await?.get_value();
        let data = ((l0 as u128) << 64) | l1 as u128;
        Ok(Self(data))
    }
}
impl GetU64 for VarInt {
    fn get_u64(&self) -> u64 {
        self.0 as u64
    }
}
impl<T> ListDataReader for Array<T> where T: DataReader + DataWriter{
    async fn read_list(reader: &mut (impl AsyncRead + Unpin), length: usize) -> Result<Self> where Self: Sized {
        let mut data = Vec::new();
        for _ in 0..length {
            data.push(T::read(reader).await?);
        }
        Ok(Self(data))
    }
}
impl<T> DataWriter for Array<T> where T: DataReader + DataWriter{
    async fn write(&self, writer: &mut (impl AsyncWrite + Unpin)) -> Result<()> {
        for e in self.0.iter() {
            e.write(writer).await?;
        }
        Ok(())
    }
}
impl ListDataReader for ByteArray {
    async fn read_list(reader: &mut (impl AsyncRead + Unpin), length: usize) -> Result<Self> where Self: Sized {
        let mut data = vec![0; length];
        match reader.read_exact(&mut data).await {
            Ok(_) => Ok(Self(data)),
            Err(_) => Error::NotEnoughtBytes(format!("{}:{}", file!(), line!())).into(),
        }
    }
}
impl DataWriter for ByteArray {
    async fn write(&self, writer: &mut (impl AsyncWrite + Unpin)) -> Result<()> {
        match writer.write_all(&self.0).await {
            Ok(_) => Ok(()),
            Err(_) => Error::FailedToWrite.into(),
        }
    }
}
impl<const S: usize> DataReader for FixedBitSet<S> {
    async fn read(reader: &mut (impl AsyncRead + Unpin)) -> Result<Self> where Self: Sized {
        let mut data = [0u8; S];
        match reader.read_exact(&mut data).await {
            Ok(_) => Ok(Self(data)),
            Err(_) => Error::NotEnoughtBytes(format!("{}:{}", file!(), line!())).into(),
        }
    }
}
impl DataReader for VarInt {
    async fn read(reader: &mut (impl AsyncRead + Unpin)) -> Result<Self> where Self: Sized {
        let mut data: i32 = 0;
        loop {
            let current = read_byte(reader, line!(), file!()).await?;
            data <<= 7;
            data |= (current & 0x7F) as i32;
            if current < 0x80 { break; }
        }
        Ok(Self(data))
    }
}
impl DataWriter for VarInt {
    async fn write(&self, writer: &mut (impl AsyncWrite + Unpin)) -> Result<()> {
        let mut data = self.0;
        loop {
            let mut current = ((data & 0x7F) as u8) | 0x80;
            data >>= 7;
            if data == 0 {
                current &= 0x7F;
                return write_bytes(writer, &[current]).await;
            }
            write_bytes(writer, &[current]).await?;
        }
    }
}
impl<T, S> DataReader for Enum<T, S> where S: DataReader + GetU64, T: ImportantEnumTrait {
    async fn read(reader: &mut (impl AsyncRead + Unpin)) -> Result<Self> where Self: Sized {
        let original_value = S::read(reader).await?;
        let value = T::new(original_value.get_u64())?;
        Ok(Self(value, original_value))
    }
}