valence_protocol/impls/
math.rs

1use std::io::Write;
2
3use valence_math::*;
4
5use crate::{Decode, Encode};
6
7impl Encode for Vec2 {
8    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
9        self.x.encode(&mut w)?;
10        self.y.encode(w)
11    }
12}
13
14impl Decode<'_> for Vec2 {
15    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
16        Ok(Self {
17            x: f32::decode(r)?,
18            y: f32::decode(r)?,
19        })
20    }
21}
22
23impl Encode for Vec3 {
24    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
25        self.x.encode(&mut w)?;
26        self.y.encode(&mut w)?;
27        self.z.encode(w)
28    }
29}
30
31impl Decode<'_> for Vec3 {
32    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
33        Ok(Self {
34            x: f32::decode(r)?,
35            y: f32::decode(r)?,
36            z: f32::decode(r)?,
37        })
38    }
39}
40
41impl Encode for Vec3A {
42    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
43        self.x.encode(&mut w)?;
44        self.y.encode(&mut w)?;
45        self.z.encode(w)
46    }
47}
48
49impl Decode<'_> for Vec3A {
50    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
51        Ok(Self::new(f32::decode(r)?, f32::decode(r)?, f32::decode(r)?))
52    }
53}
54
55impl Encode for IVec3 {
56    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
57        self.x.encode(&mut w)?;
58        self.y.encode(&mut w)?;
59        self.z.encode(w)
60    }
61}
62
63impl Decode<'_> for IVec3 {
64    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
65        Ok(Self {
66            x: i32::decode(r)?,
67            y: i32::decode(r)?,
68            z: i32::decode(r)?,
69        })
70    }
71}
72
73impl Encode for Vec4 {
74    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
75        self.x.encode(&mut w)?;
76        self.y.encode(&mut w)?;
77        self.z.encode(&mut w)?;
78        self.w.encode(&mut w)
79    }
80}
81
82impl Decode<'_> for Vec4 {
83    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
84        Ok(Self::new(
85            f32::decode(r)?,
86            f32::decode(r)?,
87            f32::decode(r)?,
88            f32::decode(r)?,
89        ))
90    }
91}
92
93impl Encode for Quat {
94    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
95        self.x.encode(&mut w)?;
96        self.y.encode(&mut w)?;
97        self.z.encode(&mut w)?;
98        self.w.encode(w)
99    }
100}
101
102impl Decode<'_> for Quat {
103    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
104        Ok(Self::from_xyzw(
105            f32::decode(r)?,
106            f32::decode(r)?,
107            f32::decode(r)?,
108            f32::decode(r)?,
109        ))
110    }
111}
112
113impl Encode for DVec2 {
114    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
115        self.x.encode(&mut w)?;
116        self.y.encode(w)
117    }
118}
119
120impl Decode<'_> for DVec2 {
121    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
122        Ok(Self {
123            x: f64::decode(r)?,
124            y: f64::decode(r)?,
125        })
126    }
127}
128
129impl Encode for DVec3 {
130    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
131        self.x.encode(&mut w)?;
132        self.y.encode(&mut w)?;
133        self.z.encode(w)
134    }
135}
136
137impl Decode<'_> for DVec3 {
138    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
139        Ok(Self {
140            x: f64::decode(r)?,
141            y: f64::decode(r)?,
142            z: f64::decode(r)?,
143        })
144    }
145}
146
147impl Encode for DQuat {
148    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
149        self.x.encode(&mut w)?;
150        self.y.encode(&mut w)?;
151        self.z.encode(&mut w)?;
152        self.w.encode(w)
153    }
154}
155
156impl Decode<'_> for DQuat {
157    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
158        Ok(Self::from_xyzw(
159            f64::decode(r)?,
160            f64::decode(r)?,
161            f64::decode(r)?,
162            f64::decode(r)?,
163        ))
164    }
165}