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 ////////////////////////////////////////////////////////////////////////////////
16
17 //! Core Authgraph operations
18
19 #![no_std]
20 extern crate alloc;
21 use alloc::vec::Vec;
22
23 pub mod arc;
24 pub mod error;
25 pub mod key;
26 pub mod keyexchange;
27 pub mod ta;
28 pub mod traits;
29
30 /// Extension trait to provide fallible-allocation variants of `Vec` methods.
31 pub trait FallibleAllocExt<T> {
32 /// Try to add the `value` to the collection, failing on memory exhaustion.
try_push(&mut self, value: T) -> Result<(), alloc::collections::TryReserveError>33 fn try_push(&mut self, value: T) -> Result<(), alloc::collections::TryReserveError>;
34 /// Try to extend the collection with the contents of `other`, failing on memory exhaustion.
try_extend_from_slice( &mut self, other: &[T], ) -> Result<(), alloc::collections::TryReserveError> where T: Clone35 fn try_extend_from_slice(
36 &mut self,
37 other: &[T],
38 ) -> Result<(), alloc::collections::TryReserveError>
39 where
40 T: Clone;
41 }
42
43 impl<T> FallibleAllocExt<T> for Vec<T> {
try_push(&mut self, value: T) -> Result<(), alloc::collections::TryReserveError>44 fn try_push(&mut self, value: T) -> Result<(), alloc::collections::TryReserveError> {
45 self.try_reserve(1)?;
46 self.push(value);
47 Ok(())
48 }
try_extend_from_slice( &mut self, other: &[T], ) -> Result<(), alloc::collections::TryReserveError> where T: Clone,49 fn try_extend_from_slice(
50 &mut self,
51 other: &[T],
52 ) -> Result<(), alloc::collections::TryReserveError>
53 where
54 T: Clone,
55 {
56 self.try_reserve(other.len())?;
57 self.extend_from_slice(other);
58 Ok(())
59 }
60 }
61
62 /// Function that mimics `vec![<val>; <len>]` but which detects allocation failure with the given
63 /// error.
vec_try_fill_with_alloc_err<T: Clone>( elem: T, len: usize, ) -> Result<Vec<T>, alloc::collections::TryReserveError>64 pub fn vec_try_fill_with_alloc_err<T: Clone>(
65 elem: T,
66 len: usize,
67 ) -> Result<Vec<T>, alloc::collections::TryReserveError> {
68 let mut v = alloc::vec::Vec::new();
69 v.try_reserve(len)?;
70 v.resize(len, elem);
71 Ok(v)
72 }
73
74 /// Macro to allocate a `Vec<T>` with the given length reserved, detecting allocation failure.
75 #[macro_export]
76 macro_rules! vec_try_with_capacity {
77 { $len:expr } => {
78 {
79 let mut v = alloc::vec::Vec::new();
80 v.try_reserve($len).map(|_| v)
81 }
82 }
83 }
84
85 /// Macro that mimics `vec![value; size]` but which detects allocation failure.
86 #[macro_export]
87 macro_rules! vec_try {
88 { $elem:expr ; $len:expr } => {
89 $crate::vec_try_fill_with_alloc_err($elem, $len)
90 };
91 }
92
93 /// Function that mimics `slice.to_vec()` but which detects allocation failures.
94 #[inline]
try_to_vec<T: Clone>(s: &[T]) -> Result<Vec<T>, alloc::collections::TryReserveError>95 pub fn try_to_vec<T: Clone>(s: &[T]) -> Result<Vec<T>, alloc::collections::TryReserveError> {
96 let mut v = vec_try_with_capacity!(s.len())?;
97 v.extend_from_slice(s);
98 Ok(v)
99 }
100