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