open_dis_rust/common/
vector3_float.rs

1//     open-dis-rust - Rust implementation of the IEEE 1278.1-2012 Distributed Interactive
2//                     Simulation (DIS) application protocol
3//     Copyright (C) 2023 Cameron Howell
4//
5//     Licensed under the BSD 2-Clause License
6
7use bytes::{Buf, BufMut, BytesMut};
8
9#[derive(Copy, Clone, Debug, Default)]
10/// Custom vector type containing 3 single precision fields
11pub struct Vector3Float {
12    /// The first value within the vector
13    pub x: f32,
14    /// The second value within the vector
15    pub y: f32,
16    /// The third value within the vector
17    pub z: f32,
18}
19
20impl Vector3Float {
21    #[must_use]
22    pub fn new(x: f32, y: f32, z: f32) -> Self {
23        Vector3Float { x, y, z }
24    }
25
26    pub fn serialize(&self, buf: &mut BytesMut) {
27        buf.put_f32(self.x);
28        buf.put_f32(self.y);
29        buf.put_f32(self.z);
30    }
31
32    pub fn decode(buf: &mut BytesMut) -> Vector3Float {
33        Vector3Float {
34            x: buf.get_f32(),
35            y: buf.get_f32(),
36            z: buf.get_f32(),
37        }
38    }
39}