data_streams/sink.rs
1// Copyright 2024 - Strixpyrr
2// SPDX-License-Identifier: Apache-2.0
3
4#[cfg(feature = "alloc")]
5use alloc::vec::Vec;
6#[cfg(all(feature = "alloc", feature = "utf8"))]
7use alloc::string::String;
8#[cfg(feature = "unstable_ascii_char")]
9use core::ascii;
10use num_traits::PrimInt;
11use bytemuck::{bytes_of, Pod};
12use crate::Result;
13
14/// A sink stream of data.
15pub trait DataSink {
16 /// Writes all bytes from `buf`. Equivalent to [`Write::write_all`].
17 ///
18 /// # Errors
19 ///
20 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
21 /// storage limit. In the case, the stream is filled completely, excluding the
22 /// overflowing bytes.
23 ///
24 /// [`Write::write_all`]: io::Write::write_all
25 ///
26 /// # Example
27 ///
28 /// ```
29 /// # use data_streams::Error;
30 /// # extern crate alloc;
31 /// # #[cfg(feature = "alloc")]
32 /// # {
33 /// # use alloc::vec::Vec;
34 /// use data_streams::DataSink;
35 ///
36 /// let mut buf = Vec::new();
37 /// buf.write_bytes(b"Hello!")?;
38 /// assert_eq!(buf, b"Hello!");
39 /// # }
40 /// # Ok::<_, Error>(())
41 /// ```
42 fn write_bytes(&mut self, buf: &[u8]) -> Result;
43 /// Writes a UTF-8 string.
44 ///
45 /// # Errors
46 ///
47 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
48 /// storage limit. In the case, the stream is filled completely, excluding the
49 /// overflowing bytes.
50 ///
51 /// # Example
52 ///
53 /// ```
54 /// # use data_streams::Error;
55 /// # extern crate alloc;
56 /// # #[cfg(feature = "alloc")]
57 /// # {
58 /// # use alloc::vec::Vec;
59 /// use data_streams::DataSink;
60 ///
61 /// let mut buf = Vec::new();
62 /// buf.write_utf8("Hello!")?;
63 /// assert_eq!(buf, b"Hello!");
64 /// # }
65 /// # Ok::<_, Error>(())
66 /// ```
67 fn write_utf8(&mut self, value: &str) -> Result {
68 self.write_bytes(value.as_bytes())
69 }
70 /// Writes a single UTF-8 codepoint.
71 ///
72 /// # Errors
73 ///
74 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
75 /// storage limit. In the case, the stream is filled completely, excluding the
76 /// overflowing bytes.
77 ///
78 /// # Example
79 ///
80 /// ```
81 /// # use data_streams::Error;
82 /// # extern crate alloc;
83 /// # #[cfg(feature = "alloc")]
84 /// # {
85 /// # use alloc::vec::Vec;
86 /// use data_streams::DataSink;
87 ///
88 /// let mut buf = Vec::new();
89 /// buf.write_utf8_codepoint('🍉')?;
90 /// assert_eq!(buf, "🍉".as_bytes());
91 /// # }
92 /// # Ok::<_, Error>(())
93 /// ```
94 fn write_utf8_codepoint(&mut self, value: char) -> Result {
95 let mut buf = [0; 4];
96 self.write_utf8(value.encode_utf8(&mut buf))
97 }
98 /// Writes an ASCII slice.
99 ///
100 /// # Errors
101 ///
102 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
103 /// storage limit. In the case, the stream is filled completely, excluding the
104 /// overflowing bytes.
105 ///
106 /// # Example
107 ///
108 /// ```
109 /// #![feature(ascii_char)]
110 /// # use data_streams::Error;
111 /// # extern crate alloc;
112 /// # #[cfg(feature = "alloc")]
113 /// # {
114 /// # use alloc::vec::Vec;
115 /// use data_streams::DataSink;
116 ///
117 /// let mut buf = Vec::new();
118 /// buf.write_ascii("Hello!".as_ascii().unwrap());
119 /// assert_eq!(buf, b"Hello!");
120 /// # }
121 /// # Ok::<_, Error>(())
122 /// ```
123 #[cfg(feature = "unstable_ascii_char")]
124 fn write_ascii(&mut self, value: &[ascii::Char]) -> Result {
125 self.write_bytes(value.as_bytes())
126 }
127
128 /// Writes a [`u8`].
129 ///
130 /// # Errors
131 ///
132 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
133 /// storage limit. In the case, the stream is filled completely, excluding the
134 /// overflowing bytes.
135 ///
136 /// # Example
137 ///
138 /// ```
139 /// # use data_streams::Error;
140 /// # extern crate alloc;
141 /// # #[cfg(feature = "alloc")]
142 /// # {
143 /// # use alloc::vec::Vec;
144 /// use data_streams::DataSink;
145 ///
146 /// let mut buf = Vec::new();
147 /// buf.write_u8(127)?;
148 /// assert_eq!(buf, [127]);
149 /// # }
150 /// # Ok::<_, Error>(())
151 /// ```
152 fn write_u8(&mut self, value: u8) -> Result { self.write_data(value) }
153 /// Writes an [`i8`].
154 ///
155 /// # Errors
156 ///
157 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
158 /// storage limit. In the case, the stream is filled completely, excluding the
159 /// overflowing bytes.
160 ///
161 /// # Example
162 ///
163 /// ```
164 /// # use data_streams::Error;
165 /// # extern crate alloc;
166 /// # #[cfg(feature = "alloc")]
167 /// # {
168 /// # use alloc::vec::Vec;
169 /// use data_streams::DataSink;
170 ///
171 /// let mut buf = Vec::new();
172 /// buf.write_i8(-127)?;
173 /// assert_eq!(buf, [-127i8 as u8]);
174 /// # }
175 /// # Ok::<_, Error>(())
176 /// ```
177 fn write_i8(&mut self, value: i8) -> Result { self.write_data(value) }
178 /// Writes a big-endian [`u16`].
179 ///
180 /// # Errors
181 ///
182 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
183 /// storage limit. In the case, the stream is filled completely, excluding the
184 /// overflowing bytes.
185 ///
186 /// # Example
187 ///
188 /// ```
189 /// # use data_streams::Error;
190 /// # extern crate alloc;
191 /// # #[cfg(feature = "alloc")]
192 /// # {
193 /// # use alloc::vec::Vec;
194 /// use data_streams::DataSink;
195 ///
196 /// let mut buf = Vec::new();
197 /// buf.write_u16(0x1234)?;
198 /// assert_eq!(buf, [0x12, 0x34]);
199 /// # }
200 /// # Ok::<_, Error>(())
201 /// ```
202 fn write_u16(&mut self, value: u16) -> Result { self.write_int(value) }
203 /// Writes a big-endian [`i16`].
204 ///
205 /// # Errors
206 ///
207 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
208 /// storage limit. In the case, the stream is filled completely, excluding the
209 /// overflowing bytes.
210 ///
211 /// # Example
212 ///
213 /// ```
214 /// # use data_streams::Error;
215 /// # extern crate alloc;
216 /// # #[cfg(feature = "alloc")]
217 /// # {
218 /// # use alloc::vec::Vec;
219 /// use data_streams::DataSink;
220 ///
221 /// let mut buf = Vec::new();
222 /// buf.write_i16(0x1234)?;
223 /// assert_eq!(buf, [0x12, 0x34]);
224 /// # }
225 /// # Ok::<_, Error>(())
226 /// ```
227 fn write_i16(&mut self, value: i16) -> Result { self.write_int(value) }
228 /// Writes a little-endian [`u16`].
229 ///
230 /// # Errors
231 ///
232 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
233 /// storage limit. In the case, the stream is filled completely, excluding the
234 /// overflowing bytes.
235 ///
236 /// # Example
237 ///
238 /// ```
239 /// # use data_streams::Error;
240 /// # extern crate alloc;
241 /// # #[cfg(feature = "alloc")]
242 /// # {
243 /// # use alloc::vec::Vec;
244 /// use data_streams::DataSink;
245 ///
246 /// let mut buf = Vec::new();
247 /// buf.write_u16_le(0x1234)?;
248 /// assert_eq!(buf, [0x34, 0x12]);
249 /// # }
250 /// # Ok::<_, Error>(())
251 /// ```
252 fn write_u16_le(&mut self, value: u16) -> Result { self.write_int_le(value) }
253 /// Writes a little-endian [`i16`].
254 ///
255 /// # Errors
256 ///
257 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
258 /// storage limit. In the case, the stream is filled completely, excluding the
259 /// overflowing bytes.
260 ///
261 /// # Example
262 ///
263 /// ```
264 /// # use data_streams::Error;
265 /// # extern crate alloc;
266 /// # #[cfg(feature = "alloc")]
267 /// # {
268 /// # use alloc::vec::Vec;
269 /// use data_streams::DataSink;
270 ///
271 /// let mut buf = Vec::new();
272 /// buf.write_i16_le(0x1234)?;
273 /// assert_eq!(buf, [0x34, 0x12]);
274 /// # }
275 /// # Ok::<_, Error>(())
276 /// ```
277 fn write_i16_le(&mut self, value: i16) -> Result { self.write_int_le(value) }
278 /// Writes a big-endian [`u32`].
279 ///
280 /// # Errors
281 ///
282 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
283 /// storage limit. In the case, the stream is filled completely, excluding the
284 /// overflowing bytes.
285 ///
286 /// # Example
287 ///
288 /// ```
289 /// # use data_streams::Error;
290 /// # extern crate alloc;
291 /// # #[cfg(feature = "alloc")]
292 /// # {
293 /// # use alloc::vec::Vec;
294 /// use data_streams::DataSink;
295 ///
296 /// let mut buf = Vec::new();
297 /// buf.write_u32(0x12345678)?;
298 /// assert_eq!(buf, [0x12, 0x34, 0x56, 0x78]);
299 /// # }
300 /// # Ok::<_, Error>(())
301 /// ```
302 fn write_u32(&mut self, value: u32) -> Result { self.write_int(value) }
303 /// Writes a big-endian [`i32`].
304 ///
305 /// # Errors
306 ///
307 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
308 /// storage limit. In the case, the stream is filled completely, excluding the
309 /// overflowing bytes.
310 ///
311 /// # Example
312 ///
313 /// ```
314 /// # use data_streams::Error;
315 /// # extern crate alloc;
316 /// # #[cfg(feature = "alloc")]
317 /// # {
318 /// # use alloc::vec::Vec;
319 /// use data_streams::DataSink;
320 ///
321 /// let mut buf = Vec::new();
322 /// buf.write_i32(0x12345678)?;
323 /// assert_eq!(buf, [0x12, 0x34, 0x56, 0x78]);
324 /// # }
325 /// # Ok::<_, Error>(())
326 /// ```
327 fn write_i32(&mut self, value: i32) -> Result { self.write_int(value) }
328 /// Writes a little-endian [`u32`].
329 ///
330 /// # Errors
331 ///
332 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
333 /// storage limit. In the case, the stream is filled completely, excluding the
334 /// overflowing bytes.
335 ///
336 /// # Example
337 ///
338 /// ```
339 /// # use data_streams::Error;
340 /// # extern crate alloc;
341 /// # #[cfg(feature = "alloc")]
342 /// # {
343 /// # use alloc::vec::Vec;
344 /// use data_streams::DataSink;
345 ///
346 /// let mut buf = Vec::new();
347 /// buf.write_u32_le(0x12345678)?;
348 /// assert_eq!(buf, [0x78, 0x56, 0x34, 0x12]);
349 /// # }
350 /// # Ok::<_, Error>(())
351 /// ```
352 fn write_u32_le(&mut self, value: u32) -> Result { self.write_int_le(value) }
353 /// Writes a little-endian [`i32`].
354 ///
355 /// # Errors
356 ///
357 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
358 /// storage limit. In the case, the stream is filled completely, excluding the
359 /// overflowing bytes.
360 ///
361 /// # Example
362 ///
363 /// ```
364 /// # use data_streams::Error;
365 /// # extern crate alloc;
366 /// # #[cfg(feature = "alloc")]
367 /// # {
368 /// # use alloc::vec::Vec;
369 /// use data_streams::DataSink;
370 ///
371 /// let mut buf = Vec::new();
372 /// buf.write_i32_le(0x12345678)?;
373 /// assert_eq!(buf, [0x78, 0x56, 0x34, 0x12]);
374 /// # }
375 /// # Ok::<_, Error>(())
376 /// ```
377 fn write_i32_le(&mut self, value: i32) -> Result { self.write_int_le(value) }
378 /// Writes a big-endian [`u64`].
379 ///
380 /// # Errors
381 ///
382 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
383 /// storage limit. In the case, the stream is filled completely, excluding the
384 /// overflowing bytes.
385 ///
386 /// # Example
387 ///
388 /// ```
389 /// # use data_streams::Error;
390 /// # extern crate alloc;
391 /// # #[cfg(feature = "alloc")]
392 /// # {
393 /// # use alloc::vec::Vec;
394 /// use data_streams::DataSink;
395 ///
396 /// let mut buf = Vec::new();
397 /// buf.write_u64(0x1234_5678_9ABC_DEF0)?;
398 /// assert_eq!(buf, [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]);
399 /// # }
400 /// # Ok::<_, Error>(())
401 /// ```
402 fn write_u64(&mut self, value: u64) -> Result { self.write_int(value) }
403 /// Writes a big-endian [`i64`].
404 ///
405 /// # Errors
406 ///
407 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
408 /// storage limit. In the case, the stream is filled completely, excluding the
409 /// overflowing bytes.
410 ///
411 /// # Example
412 ///
413 /// ```
414 /// # use data_streams::Error;
415 /// # extern crate alloc;
416 /// # #[cfg(feature = "alloc")]
417 /// # {
418 /// # use alloc::vec::Vec;
419 /// use data_streams::DataSink;
420 ///
421 /// let mut buf = Vec::new();
422 /// buf.write_i64(0x1234_5678_9ABC_DEF0)?;
423 /// assert_eq!(buf, [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]);
424 /// # }
425 /// # Ok::<_, Error>(())
426 /// ```
427 fn write_i64(&mut self, value: i64) -> Result { self.write_int(value) }
428 /// Writes a little-endian [`u64`].
429 ///
430 /// # Errors
431 ///
432 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
433 /// storage limit. In the case, the stream is filled completely, excluding the
434 /// overflowing bytes.
435 ///
436 /// # Example
437 ///
438 /// ```
439 /// # use data_streams::Error;
440 /// # extern crate alloc;
441 /// # #[cfg(feature = "alloc")]
442 /// # {
443 /// # use alloc::vec::Vec;
444 /// use data_streams::DataSink;
445 ///
446 /// let mut buf = Vec::new();
447 /// buf.write_u64_le(0x1234_5678_9ABC_DEF0)?;
448 /// assert_eq!(buf, [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]);
449 /// # }
450 /// # Ok::<_, Error>(())
451 /// ```
452 fn write_u64_le(&mut self, value: u64) -> Result { self.write_int_le(value) }
453 /// Writes a little-endian [`i64`].
454 ///
455 /// # Errors
456 ///
457 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
458 /// storage limit. In the case, the stream is filled completely, excluding the
459 /// overflowing bytes.
460 ///
461 /// # Example
462 ///
463 /// ```
464 /// # use data_streams::Error;
465 /// # extern crate alloc;
466 /// # #[cfg(feature = "alloc")]
467 /// # {
468 /// # use alloc::vec::Vec;
469 /// use data_streams::DataSink;
470 ///
471 /// let mut buf = Vec::new();
472 /// buf.write_i64_le(0x1234_5678_9ABC_DEF0)?;
473 /// assert_eq!(buf, [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]);
474 /// # }
475 /// # Ok::<_, Error>(())
476 /// ```
477 fn write_i64_le(&mut self, value: i64) -> Result { self.write_int_le(value) }
478 /// Writes a big-endian [`u128`].
479 ///
480 /// # Errors
481 ///
482 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
483 /// storage limit. In the case, the stream is filled completely, excluding the
484 /// overflowing bytes.
485 ///
486 /// # Example
487 ///
488 /// ```
489 /// # use data_streams::Error;
490 /// # extern crate alloc;
491 /// # #[cfg(feature = "alloc")]
492 /// # {
493 /// # use alloc::vec::Vec;
494 /// use data_streams::DataSink;
495 ///
496 /// let mut buf = Vec::new();
497 /// buf.write_u128(0x1234_5678_9ABC_DEF0_0FED_CBA9_8765_4321)?;
498 /// assert_eq!(buf, [
499 /// 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
500 /// 0x0F, 0xED, 0xCB, 0xA9, 0x87, 0x65, 0x43, 0x21
501 /// ]);
502 /// # }
503 /// # Ok::<_, Error>(())
504 /// ```
505 fn write_u128(&mut self, value: u128) -> Result { self.write_int(value) }
506 /// Writes a big-endian [`i128`].
507 ///
508 /// # Errors
509 ///
510 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
511 /// storage limit. In the case, the stream is filled completely, excluding the
512 /// overflowing bytes.
513 ///
514 /// # Example
515 ///
516 /// ```
517 /// # use data_streams::Error;
518 /// # extern crate alloc;
519 /// # #[cfg(feature = "alloc")]
520 /// # {
521 /// # use alloc::vec::Vec;
522 /// use data_streams::DataSink;
523 ///
524 /// let mut buf = Vec::new();
525 /// buf.write_i128(0x1234_5678_9ABC_DEF0_0FED_CBA9_8765_4321)?;
526 /// assert_eq!(buf, [
527 /// 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
528 /// 0x0F, 0xED, 0xCB, 0xA9, 0x87, 0x65, 0x43, 0x21
529 /// ]);
530 /// # }
531 /// # Ok::<_, Error>(())
532 /// ```
533 fn write_i128(&mut self, value: i128) -> Result { self.write_int(value) }
534 /// Writes a little-endian [`u128`].
535 ///
536 /// # Errors
537 ///
538 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
539 /// storage limit. In the case, the stream is filled completely, excluding the
540 /// overflowing bytes.
541 ///
542 /// # Example
543 ///
544 /// ```
545 /// # use data_streams::Error;
546 /// # extern crate alloc;
547 /// # #[cfg(feature = "alloc")]
548 /// # {
549 /// # use alloc::vec::Vec;
550 /// use data_streams::DataSink;
551 ///
552 /// let mut buf = Vec::new();
553 /// buf.write_u128_le(0x1234_5678_9ABC_DEF0_0FED_CBA9_8765_4321)?;
554 /// assert_eq!(buf, [
555 /// 0x21, 0x43, 0x65, 0x87, 0xA9, 0xCB, 0xED, 0x0F,
556 /// 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12
557 /// ]);
558 /// # }
559 /// # Ok::<_, Error>(())
560 /// ```
561 fn write_u128_le(&mut self, value: u128) -> Result { self.write_int_le(value) }
562 /// Writes a little-endian [`i128`].
563 ///
564 /// # Errors
565 ///
566 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
567 /// storage limit. In the case, the stream is filled completely, excluding the
568 /// overflowing bytes.
569 ///
570 /// # Example
571 ///
572 /// ```
573 /// # use data_streams::Error;
574 /// # extern crate alloc;
575 /// # #[cfg(feature = "alloc")]
576 /// # {
577 /// # use alloc::vec::Vec;
578 /// use data_streams::DataSink;
579 ///
580 /// let mut buf = Vec::new();
581 /// buf.write_i128_le(0x1234_5678_9ABC_DEF0_0FED_CBA9_8765_4321)?;
582 /// assert_eq!(buf, [
583 /// 0x21, 0x43, 0x65, 0x87, 0xA9, 0xCB, 0xED, 0x0F,
584 /// 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12
585 /// ]);
586 /// # }
587 /// # Ok::<_, Error>(())
588 /// ```
589 fn write_i128_le(&mut self, value: i128) -> Result { self.write_int_le(value) }
590 /// Writes a big-endian [`usize`]. To make streams consistent across platforms,
591 /// [`usize`] is fixed to the size of [`u64`] regardless of the target platform.
592 ///
593 /// # Errors
594 ///
595 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
596 /// storage limit. In the case, the stream is filled completely, excluding the
597 /// overflowing bytes.
598 ///
599 /// # Example
600 ///
601 /// ```
602 /// # use data_streams::Error;
603 /// # extern crate alloc;
604 /// # #[cfg(all(feature = "alloc", target_pointer_width = "64"))]
605 /// # {
606 /// # use alloc::vec::Vec;
607 /// use data_streams::DataSink;
608 ///
609 /// let mut buf = Vec::new();
610 /// buf.write_usize(0x1234_5678_9ABC_DEF0)?;
611 /// assert_eq!(buf, [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]);
612 /// # }
613 /// # Ok::<_, Error>(())
614 /// ```
615 fn write_usize(&mut self, value: usize) -> Result {
616 self.write_u64(value as u64)
617 }
618 /// Writes a big-endian [`isize`]. To make streams consistent across platforms,
619 /// [`isize`] is fixed to the size of [`i64`] regardless of the target platform.
620 ///
621 /// # Errors
622 ///
623 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
624 /// storage limit. In the case, the stream is filled completely, excluding the
625 /// overflowing bytes.
626 ///
627 /// # Example
628 ///
629 /// ```
630 /// # use data_streams::Error;
631 /// # extern crate alloc;
632 /// # #[cfg(all(feature = "alloc", target_pointer_width = "64"))]
633 /// # {
634 /// # use alloc::vec::Vec;
635 /// use data_streams::DataSink;
636 ///
637 /// let mut buf = Vec::new();
638 /// buf.write_isize(0x1234_5678_9ABC_DEF0)?;
639 /// assert_eq!(buf, [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]);
640 /// # }
641 /// # Ok::<_, Error>(())
642 /// ```
643 fn write_isize(&mut self, value: isize) -> Result {
644 self.write_i64(value as i64)
645 }
646 /// Writes a little-endian [`usize`]. To make streams consistent across platforms,
647 /// [`usize`] is fixed to the size of [`u64`] regardless of the target platform.
648 ///
649 /// # Errors
650 ///
651 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
652 /// storage limit. In the case, the stream is filled completely, excluding the
653 /// overflowing bytes.
654 ///
655 /// # Example
656 ///
657 /// ```
658 /// # use data_streams::Error;
659 /// # extern crate alloc;
660 /// # #[cfg(all(feature = "alloc", target_pointer_width = "64"))]
661 /// # {
662 /// # use alloc::vec::Vec;
663 /// use data_streams::DataSink;
664 ///
665 /// let mut buf = Vec::new();
666 /// buf.write_usize_le(0x1234_5678_9ABC_DEF0)?;
667 /// assert_eq!(buf, [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]);
668 /// # }
669 /// # Ok::<_, Error>(())
670 /// ```
671 fn write_usize_le(&mut self, value: usize) -> Result {
672 self.write_u64_le(value as u64)
673 }
674 /// Writes a little-endian [`isize`]. To make streams consistent across platforms,
675 /// [`isize`] is fixed to the size of [`i64`] regardless of the target platform.
676 ///
677 /// # Errors
678 ///
679 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
680 /// storage limit. In the case, the stream is filled completely, excluding the
681 /// overflowing bytes.
682 ///
683 /// # Example
684 ///
685 /// ```
686 /// # use data_streams::Error;
687 /// # extern crate alloc;
688 /// # #[cfg(all(feature = "alloc", target_pointer_width = "64"))]
689 /// # {
690 /// # use alloc::vec::Vec;
691 /// use data_streams::DataSink;
692 ///
693 /// let mut buf = Vec::new();
694 /// buf.write_isize_le(0x1234_5678_9ABC_DEF0)?;
695 /// assert_eq!(buf, [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]);
696 /// # }
697 /// # Ok::<_, Error>(())
698 /// ```
699 fn write_isize_le(&mut self, value: isize) -> Result {
700 self.write_i64_le(value as i64)
701 }
702}
703
704/// Writes generic data to a [sink](DataSink).
705pub trait GenericDataSink: DataSink {
706 /// Writes a big-endian integer.
707 ///
708 /// # Errors
709 ///
710 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
711 /// storage limit. In the case, the stream is filled completely, excluding the
712 /// overflowing bytes.
713 ///
714 /// # Example
715 ///
716 /// ```
717 /// # use data_streams::Error;
718 /// # extern crate alloc;
719 /// # #[cfg(feature = "alloc")]
720 /// # {
721 /// # use alloc::vec::Vec;
722 /// use data_streams::GenericDataSink;
723 ///
724 /// let mut buf = Vec::new();
725 /// buf.write_int(0x12345678)?;
726 /// assert_eq!(buf, [0x12, 0x34, 0x56, 0x78]);
727 /// # }
728 /// # Ok::<_, Error>(())
729 /// ```
730 fn write_int<T: Pod + PrimInt>(&mut self, value: T) -> Result {
731 self.write_data(value.to_be())
732 }
733 /// Writes a little-endian integer.
734 ///
735 /// # Errors
736 ///
737 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
738 /// storage limit. In the case, the stream is filled completely, excluding the
739 /// overflowing bytes.
740 ///
741 /// # Example
742 ///
743 /// ```
744 /// # use data_streams::Error;
745 /// # extern crate alloc;
746 /// # #[cfg(feature = "alloc")]
747 /// # {
748 /// # use alloc::vec::Vec;
749 /// use data_streams::GenericDataSink;
750 ///
751 /// let mut buf = Vec::new();
752 /// buf.write_int_le(0x12345678)?;
753 /// assert_eq!(buf, [0x78, 0x56, 0x34, 0x12]);
754 /// # }
755 /// # Ok::<_, Error>(())
756 /// ```
757 fn write_int_le<T: Pod + PrimInt>(&mut self, value: T) -> Result {
758 self.write_data(value.to_le())
759 }
760 /// Writes a value of an arbitrary bit pattern. See [`Pod`].
761 ///
762 /// # Errors
763 ///
764 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
765 /// storage limit. In the case, the stream is filled completely, excluding the
766 /// overflowing bytes.
767 ///
768 /// # Example
769 ///
770 /// ```
771 /// # use data_streams::Error;
772 /// # extern crate alloc;
773 /// # #[cfg(all(feature = "alloc", target_endian = "little"))]
774 /// # {
775 /// # use alloc::vec::Vec;
776 /// use data_streams::GenericDataSink;
777 ///
778 /// let mut buf = Vec::new();
779 /// buf.write_data(0x12345678)?;
780 /// assert_eq!(buf, [0x78, 0x56, 0x34, 0x12]);
781 /// # }
782 /// # Ok::<_, Error>(())
783 /// ```
784 fn write_data<T: Pod>(&mut self, value: T) -> Result {
785 self.write_bytes(bytes_of(&value))
786 }
787}
788
789impl<S: DataSink + ?Sized> GenericDataSink for S { }
790
791/// A sink stream of vector data.
792#[cfg(feature = "alloc")]
793pub trait VecSink: DataSink {
794 /// Writes all bytes from a [`Vec`].
795 ///
796 /// # Errors
797 ///
798 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
799 /// storage limit. In the case, the stream is filled completely, excluding the
800 /// overflowing bytes.
801 ///
802 /// # Examples
803 ///
804 /// ```
805 /// # extern crate alloc;
806 /// # use alloc::vec::Vec;
807 /// # use data_streams::Error;
808 /// use data_streams::VecSink;
809 ///
810 /// let mut buf = Vec::new();
811 /// buf.write_owned_bytes(b"Hello!".into())?;
812 /// assert_eq!(buf, b"Hello!");
813 /// # Ok::<_, Error>(())
814 /// ```
815 ///
816 /// # Implementation
817 ///
818 /// By default, this delegates to [`write_bytes`]. Some implementations may be
819 /// may better optimize for owned data.
820 ///
821 /// [`write_bytes`]: DataSink::write_bytes
822 fn write_owned_bytes(&mut self, buf: Vec<u8>) -> Result;
823 /// Writes all UTF-8 bytes from a [`String`].
824 ///
825 /// # Errors
826 ///
827 /// May return [`Overflow`](Error::Overflow) if the sink would exceed some hard
828 /// storage limit. In the case, the stream is filled completely, excluding the
829 /// overflowing bytes.
830 ///
831 /// # Examples
832 ///
833 /// ```
834 /// # extern crate alloc;
835 /// # use alloc::vec::Vec;
836 /// # use data_streams::Error;
837 /// use data_streams::VecSink;
838 ///
839 /// let mut buf = Vec::new();
840 /// buf.write_owned_utf8("Hello!".into())?;
841 /// assert_eq!(buf, b"Hello!");
842 /// # Ok::<_, Error>(())
843 /// ```
844 ///
845 /// # Implementation
846 ///
847 /// By default, this delegates to [`write_utf8`]. Some implementations may be
848 /// may better optimize for owned data.
849 ///
850 /// [`write_utf8`]: DataSink::write_utf8
851 #[cfg(feature = "utf8")]
852 fn write_owned_utf8(&mut self, buf: String) -> Result;
853}
854
855#[cfg(all(feature = "alloc", feature = "unstable_specialization"))]
856impl<T: DataSink> VecSink for T {
857 default fn write_owned_bytes(&mut self, buf: Vec<u8>) -> Result {
858 self.write_bytes(&buf)
859 }
860
861 #[cfg(feature = "utf8")]
862 default fn write_owned_utf8(&mut self, buf: String) -> Result {
863 self.write_utf8(&buf)
864 }
865}
866
867#[cfg(all(feature = "alloc", not(feature = "unstable_specialization")))]
868impl<T: DataSink> VecSink for T {
869 fn write_owned_bytes(&mut self, buf: Vec<u8>) -> Result {
870 self.write_bytes(&buf)
871 }
872
873 #[cfg(feature = "utf8")]
874 fn write_owned_utf8(&mut self, buf: String) -> Result {
875 self.write_utf8(&buf)
876 }
877}