Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / third_party / betocore / sink / src / lib.rs

lib.rs106 lines · 3.3 KiBRaw
1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A no_std-friendly data-writing "sink" trait which allows for convenient expression
16//! of "write me into a limited-size buffer"-type methods on traits.
17
18#![cfg_attr(not(feature = "std"), no_std)]
19
20/// An append-only, limited-size collection.
21pub trait Sink<T> {
22 /// Returns `Some` if the slice was appended, `None` otherwise, in which case nothing was written
23 fn try_extend_from_slice(&mut self, items: &[T]) -> Option<()>;
24
25 /// Returns `Some` if the item could be pushed, `None` otherwise
26 fn try_push(&mut self, item: T) -> Option<()>;
27
28 /// Uses the given [`SinkWriter`] to write to this sink.
29 fn try_extend_from_writer<W: SinkWriter<DataType = T>>(&mut self, writer: W) -> Option<()>
30 where
31 Self: Sized,
32 {
33 writer.write_payload(self)
34 }
35}
36
37/// A use-once trait (like `FnOnce`) which represents some
38/// code which writes data to a [`Sink`].
39pub trait SinkWriter {
40 /// The type of data being written to the [`Sink`].
41 type DataType;
42
43 /// Returns `Some` if all data was successfully written to the [`Sink`],
44 /// but if doing so failed at any point, returns `None`. If this method
45 /// fails, the contents of the [`Sink`] should be considered to be invalid.
46 fn write_payload<S: Sink<Self::DataType>>(self, sink: &mut S) -> Option<()>;
47}
48
49impl<T> Sink<T> for tinyvec::SliceVec<'_, T>
50where
51 T: Clone,
52{
53 fn try_extend_from_slice(&mut self, items: &[T]) -> Option<()> {
54 if items.len() > (self.capacity() - self.len()) {
55 return None;
56 }
57 // won't panic: just checked the length
58 self.extend_from_slice(items);
59 Some(())
60 }
61 fn try_push(&mut self, item: T) -> Option<()> {
62 if self.len() == self.capacity() {
63 None
64 } else {
65 self.push(item);
66 Some(())
67 }
68 }
69}
70
71impl<T, A> Sink<T> for tinyvec::ArrayVec<A>
72where
73 A: tinyvec::Array<Item = T>,
74 T: Clone,
75{
76 fn try_extend_from_slice(&mut self, items: &[T]) -> Option<()> {
77 if items.len() > (self.capacity() - self.len()) {
78 return None;
79 }
80 // won't panic: just checked the length
81 self.extend_from_slice(items);
82 Some(())
83 }
84
85 fn try_push(&mut self, item: T) -> Option<()> {
86 // tinyvec uses None to indicate success, whereas for our limited purposes we want the
87 // opposite
88 match tinyvec::ArrayVec::try_push(self, item) {
89 None => Some(()),
90 Some(_) => None,
91 }
92 }
93}
94
95#[cfg(feature = "std")]
96impl<T: Clone> Sink<T> for std::vec::Vec<T> {
97 fn try_extend_from_slice(&mut self, items: &[T]) -> Option<()> {
98 self.extend_from_slice(items);
99 Some(())
100 }
101
102 fn try_push(&mut self, item: T) -> Option<()> {
103 self.push(item);
104 Some(())
105 }
106}