simple_bytes/
bytes_write.rs

1
2use crate::Bytes;
3
4use std::fmt;
5
6macro_rules! write_fn {
7	($name:ident, $try_name:ident, $type:ident) => (
8		write_fn!($name, $try_name, $type, stringify!($type));
9	);
10	($name:ident, $try_name:ident, $type:ident, $type_str:expr) => {
11		#[inline]
12		#[doc = "Try to write "]
13		#[doc = $type_str]
14		#[doc = " in big-endian.`"]
15		fn $try_name(&mut self, num: $type) -> Result<(), WriteError> {
16			self.try_write(num.to_be_bytes())
17		}
18
19		#[inline]
20		#[track_caller]
21		#[doc = "Writes an `"]
22		#[doc = $type_str]
23		#[doc = "` in big-endian."]
24		/// 
25		/// ## Panics
26		/// If there aren't enough remaining bytes left.
27		fn $name(&mut self, num: $type) {
28			self.$try_name(num).expect("failed to write")
29		}
30	}
31}
32
33macro_rules! write_le_fn {
34	($name:ident, $try_name:ident, $type:ident) => (
35		write_le_fn!($name, $try_name, $type, stringify!($type));
36	);
37	($name:ident, $try_name:ident, $type:ident, $type_str:expr) => {
38		#[inline]
39		#[doc = "Try to write "]
40		#[doc = $type_str]
41		#[doc = " in little-endian.`"]
42		fn $try_name(&mut self, num: $type) -> Result<(), WriteError> {
43			self.try_write(num.to_le_bytes())
44		}
45
46		#[inline]
47		#[track_caller]
48		#[doc = "Writes an `"]
49		#[doc = $type_str]
50		#[doc = "` in little-endian."]
51		/// 
52		/// ## Panics
53		/// If there aren't enough remaining bytes left.
54		fn $name(&mut self, num: $type) {
55			self.$try_name(num).expect("failed to write")
56		}
57	}
58}
59
60/// Get's returned when there is not enough space to write everything.
61/// If this get's returned nothing should be written.
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub struct WriteError;
64
65impl fmt::Display for WriteError {
66	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67		fmt::Debug::fmt(self, f)
68	}
69}
70
71impl std::error::Error for WriteError {}
72
73/// Write bytes or numbers.
74pub trait BytesWrite {
75	/// Returns the entire slice mutably.
76	fn as_mut(&mut self) -> &mut [u8];
77
78	/// Returns the entire slice as a bytes struct
79	/// setting the position of the new Bytes to `0`.
80	fn as_bytes(&self) -> Bytes<'_>;
81
82	/// Returns the remaining bytes mutably.
83	fn remaining_mut(&mut self) -> &mut [u8];
84
85	/// Writes a slice.
86	fn try_write(&mut self, slice: impl AsRef<[u8]>) -> Result<(), WriteError>;
87
88	/// Writes a slice.
89	/// 
90	/// ## Panics
91	/// If there aren't enough remaining bytes left.
92	#[track_caller]
93	fn write(&mut self, slice: impl AsRef<[u8]>) {
94		self.try_write(slice).expect("failed to write")
95	}
96
97	write_fn!(write_u8, try_write_u8, u8);
98	write_fn!(write_u16, try_write_u16, u16);
99	write_fn!(write_u32, try_write_u32, u32);
100	write_fn!(write_u64, try_write_u64, u64);
101	write_fn!(write_u128, try_write_u128, u128);
102
103	write_fn!(write_i8, try_write_i8, i8);
104	write_fn!(write_i16, try_write_i16, i16);
105	write_fn!(write_i32, try_write_i32, i32);
106	write_fn!(write_i64, try_write_i64, i64);
107	write_fn!(write_i128, try_write_i128, i128);
108
109	write_fn!(write_f32, try_write_f32, f32);
110	write_fn!(write_f64, try_write_f64, f64);
111
112	write_le_fn!(write_le_u8, try_write_le_u8, u8);
113	write_le_fn!(write_le_u16, try_write_le_u16, u16);
114	write_le_fn!(write_le_u32, try_write_le_u32, u32);
115	write_le_fn!(write_le_u64, try_write_le_u64, u64);
116	write_le_fn!(write_le_u128, try_write_le_u128, u128);
117
118	write_le_fn!(write_le_i8, try_write_le_i8, i8);
119	write_le_fn!(write_le_i16, try_write_le_i16, i16);
120	write_le_fn!(write_le_i32, try_write_le_i32, i32);
121	write_le_fn!(write_le_i64, try_write_le_i64, i64);
122	write_le_fn!(write_le_i128, try_write_le_i128, i128);
123
124	write_le_fn!(write_le_f32, try_write_le_f32, f32);
125	write_le_fn!(write_le_f64, try_write_le_f64, f64);
126}
127
128impl<W: BytesWrite> BytesWrite for &mut W {
129	#[inline]
130	fn as_mut(&mut self) -> &mut [u8] {
131		(**self).as_mut()
132	}
133
134	#[inline]
135	fn as_bytes(&self) -> Bytes<'_> {
136		(**self).as_bytes()
137	}
138
139	#[inline]
140	fn remaining_mut(&mut self) -> &mut [u8] {
141		(**self).remaining_mut()
142	}
143
144	#[inline]
145	fn try_write(&mut self, slice: impl AsRef<[u8]>) -> Result<(), WriteError> {
146		(**self).try_write(slice)
147	}
148}