Skip to main content

small_fixed_array/
truncating_into.rs

1use alloc::{string::String, vec::Vec};
2
3use crate::{FixedArray, FixedString, ValidLength};
4
5mod sealed {
6    use alloc::{string::String, vec::Vec};
7
8    #[allow(unnameable_types)]
9    pub trait Sealed {}
10
11    impl Sealed for String {}
12    impl<T> Sealed for Vec<T> {}
13}
14
15/// A sealed helper trait for calling [`FixedArray<T>::from_vec_trunc`] or [`FixedString::from_string_trunc`].
16///
17/// Both of these functions may truncate the input in order to fit it into the provided [`ValidLength`],
18/// therefore this trait must be imported in order to make possible truncation made obvious in user code.
19pub trait TruncatingInto<T>: sealed::Sealed {
20    fn trunc_into(self) -> T;
21}
22
23impl<LenT: ValidLength> TruncatingInto<FixedString<LenT>> for String {
24    fn trunc_into(self) -> FixedString<LenT> {
25        FixedString::from_string_trunc(self)
26    }
27}
28
29impl<T, LenT: ValidLength> TruncatingInto<FixedArray<T, LenT>> for Vec<T> {
30    fn trunc_into(self) -> FixedArray<T, LenT> {
31        FixedArray::from_vec_trunc(self)
32    }
33}