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
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::marker::PhantomData;
use euclid::default::Vector3D;
pub type Angle = euclid::Angle<f32>;
pub type BlockPosition = Vector3D<i32>;
pub struct ReadRemainingBytesArray<T> {
pub value: Vec<T>,
}
pub struct WriteRemainingBytesArray<'a, T> {
pub value: &'a Vec<T>,
}
impl<T> ReadRemainingBytesArray<T> {
pub fn new(value: Vec<T>) -> Self {
Self { value }
}
}
impl<T> From<ReadRemainingBytesArray<T>> for Vec<T> {
fn from(value: ReadRemainingBytesArray<T>) -> Self {
value.value
}
}
impl<'a, T> From<&'a Vec<T>> for WriteRemainingBytesArray<'a, T> {
fn from(value: &'a Vec<T>) -> Self {
Self { value }
}
}
pub struct ReadLengthProvidedArray<T, S> {
pub value: Vec<T>,
size: PhantomData<S>,
}
pub struct WriteLengthProvidedArray<'a, T, S> {
pub value: &'a Vec<T>,
size: PhantomData<S>,
}
impl<T, S> ReadLengthProvidedArray<T, S> {
pub fn new(value: Vec<T>) -> Self {
Self { value, size: PhantomData }
}
}
impl<T, S> From<ReadLengthProvidedArray<T, S>> for Vec<T> {
fn from(value: ReadLengthProvidedArray<T, S>) -> Self {
value.value
}
}
impl<'a, T, S> From<&'a Vec<T>> for WriteLengthProvidedArray<'a, T, S> {
fn from(value: &'a Vec<T>) -> Self {
Self { value, size: PhantomData }
}
}
macro_rules! advanced_container_type {
($write: ident, $read: ident) => {
pub struct $read<T> {
pub value: T
}
pub struct $write<'a, T> {
pub value: &'a T
}
impl<T> $read<T> {
pub fn into(self) -> T {
self.value
}
}
impl<'a, T> $write<'a, T> {
pub fn from(value: &'a T) -> Self {
Self { value }
}
}
}
}
macro_rules! container_type {
($name: ident, $contained: ty) => {
#[derive(Debug, Copy, Clone)]
pub struct $name(pub $contained);
impl From<$name> for $contained {
fn from(value: $name) -> Self {
value.0
}
}
impl From<$contained> for $name {
fn from(value: $contained) -> Self {
$name(value)
}
}
impl From<& $contained> for $name {
fn from(value: & $contained) -> Self {
$name(*value)
}
}
}
}
advanced_container_type!(WriteProtocolJson, ReadProtocolJson);
advanced_container_type!(WriteProtocolNbt, ReadProtocolNbt);
container_type!(VarInt, i32);
container_type!(VarLong, i64);