-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface_packet.rs
More file actions
86 lines (74 loc) · 2.45 KB
/
interface_packet.rs
File metadata and controls
86 lines (74 loc) · 2.45 KB
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
use crate::{FromGil, IntoGil, PyDefault, flat, flat_err_to_py};
use planus::{Builder, ReadAsRoot};
use pyo3::{prelude::*, types::*};
#[pyclass(module = "rlbot_flatbuffers", subclass, frozen, get_all)]
pub struct InterfacePacket {
pub message: Py<PyAny>,
}
impl crate::PyDefault for InterfacePacket {
fn py_default(py: Python) -> Py<Self> {
Py::new(
py,
Self {
message: super::InterfaceMessage::py_default(py),
},
)
.unwrap()
}
}
impl FromGil<&flat::InterfacePacket> for InterfacePacket {
#[allow(unused_variables)]
fn from_gil(py: Python, flat_t: &flat::InterfacePacket) -> Self {
InterfacePacket {
message: IntoGil::<super::InterfaceMessage>::into_gil(&flat_t.message, py).into_any(),
}
}
}
impl FromGil<&InterfacePacket> for flat::InterfacePacket {
#[allow(unused_variables)]
fn from_gil(py: Python, py_type: &InterfacePacket) -> Self {
Self {
message: super::InterfaceMessage::extract(py_type.message.bind_borrowed(py))
.as_ref()
.unwrap()
.into_gil(py),
}
}
}
#[pymethods]
impl InterfacePacket {
#[new]
#[pyo3(signature = (message=None))]
pub fn new(py: Python, message: Option<Py<PyAny>>) -> Self {
Self {
message: message.unwrap_or_else(|| super::InterfaceMessage::py_default(py)),
}
}
pub fn __str__(&self, py: Python) -> String {
self.__repr__(py)
}
#[allow(unused_variables)]
pub fn __repr__(&self, py: Python) -> String {
format!(
"InterfacePacket(message={})",
super::InterfaceMessage::extract(self.message.bind_borrowed(py))
.unwrap()
.__repr__(py),
)
}
#[classattr]
fn __match_args__() -> (&'static str,) {
("message",)
}
fn pack<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
let mut builder = Builder::with_capacity(u16::MAX as usize);
let flat_t = flat::InterfacePacket::from_gil(py, self);
PyBytes::new(py, builder.finish(flat_t, None))
}
#[staticmethod]
fn unpack(py: Python, data: &[u8]) -> PyResult<Py<Self>> {
let flat_t_ref = flat::InterfacePacketRef::read_as_root(data).map_err(flat_err_to_py)?;
let flat_t = flat::InterfacePacket::try_from(flat_t_ref).map_err(flat_err_to_py)?;
Ok(crate::into_py_from(py, &flat_t))
}
}