Skip to main content

gaia_types/assembler/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use byteorder::{ByteOrder, WriteBytesExt};
4use std::{
5    io::{Seek, SeekFrom, Write},
6    marker::PhantomData,
7};
8
9/// 二进制写入器,用于从实现了 WriteBytesExt trait 的类型中写入数据
10///
11/// 这是一个泛型结构体,可以包装任何实现了 WriteBytesExt trait 的类型,
12/// 提供二进制数据的写入功能。
13#[derive(Copy, Clone, Debug)]
14pub struct BinaryWriter<W, E> {
15    writer: W,
16    endian: PhantomData<E>,
17}
18
19impl<W: Write, E> Write for BinaryWriter<W, E> {
20    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
21        self.writer.write(buf)
22    }
23
24    fn flush(&mut self) -> std::io::Result<()> {
25        self.writer.flush()
26    }
27}
28
29impl<W: Seek, E> Seek for BinaryWriter<W, E> {
30    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
31        self.writer.seek(pos)
32    }
33}
34
35impl<W, E> BinaryWriter<W, E> {
36    /// 创建一个新的二进制写入器
37    pub fn new(writer: W) -> Self {
38        Self { writer, endian: PhantomData }
39    }
40    /// 获取内部写入器
41    pub fn finish(self) -> W {
42        self.writer
43    }
44}
45
46impl<W: Write, E> BinaryWriter<W, E> {
47    /// 将一个 u8 值写入到字节流中。
48    ///
49    /// # 参数
50    ///
51    /// * `value` - 要写入的 u8 值。
52    pub fn write_u8(&mut self, value: u8) -> std::io::Result<()>
53    where
54        W: Write,
55    {
56        self.writer.write_u8(value)
57    }
58
59    /// 将一个 u16 值写入到字节流中。
60    ///
61    /// # 参数
62    ///
63    /// * `value` - 要写入的 u16 值。
64    pub fn write_u16(&mut self, value: u16) -> std::io::Result<()>
65    where
66        W: Write,
67        E: ByteOrder,
68    {
69        self.writer.write_u16::<E>(value)
70    }
71
72    /// 将一个 u32 值写入到字节流中。
73    ///
74    /// # 参数
75    ///
76    /// * `value` - 要写入的 u32 值。
77    pub fn write_u32(&mut self, value: u32) -> std::io::Result<()>
78    where
79        W: Write,
80        E: ByteOrder,
81    {
82        self.writer.write_u32::<E>(value)
83    }
84
85    /// 将一个 u64 值写入到字节流中。
86    ///
87    /// # 参数
88    ///
89    /// * `value` - 要写入的 u64 值。
90    pub fn write_u64(&mut self, value: u64) -> std::io::Result<()>
91    where
92        W: Write,
93        E: ByteOrder,
94    {
95        self.writer.write_u64::<E>(value)
96    }
97
98    /// 将一个 i16 值写入到字节流中。
99    pub fn write_i16(&mut self, value: i16) -> std::io::Result<()>
100    where
101        W: Write,
102        E: ByteOrder,
103    {
104        self.writer.write_i16::<E>(value)
105    }
106
107    /// 将一个 i32 值写入到字节流中。
108    pub fn write_i32(&mut self, value: i32) -> std::io::Result<()>
109    where
110        W: Write,
111        E: ByteOrder,
112    {
113        self.writer.write_i32::<E>(value)
114    }
115
116    /// 将一个 i64 值写入到字节流中。
117    pub fn write_i64(&mut self, value: i64) -> std::io::Result<()>
118    where
119        W: Write,
120        E: ByteOrder,
121    {
122        self.writer.write_i64::<E>(value)
123    }
124
125    /// 将字节数组写入到字节流中。
126    ///
127    /// # 参数
128    ///
129    /// * `bytes` - 要写入的字节数组。
130    pub fn write_bytes(&mut self, bytes: &[u8]) -> std::io::Result<()>
131    where
132        W: Write,
133    {
134        self.writer.write_all(bytes)
135    }
136}