1#![forbid(unsafe_code)]
16#![deny(missing_docs)]
17#![cfg_attr(not(feature = "std"), no_std)]
18#![cfg_attr(docsrs, feature(doc_auto_cfg))]
19
20#[cfg(feature = "std")]
21use std::borrow::{Cow, ToOwned};
22
23#[cfg(feature = "alloc")]
24extern crate alloc;
25
26#[cfg(all(not(feature = "std"), feature = "alloc"))]
27use alloc::{
28 borrow::{Cow, ToOwned},
29 vec::Vec,
30};
31
32pub trait IntoStatic {
34 type Static: IntoStatic<Static = Self::Static> + 'static;
36
37 fn into_static(self) -> Self::Static;
39}
40
41#[cfg(any(feature = "std", feature = "alloc"))]
42impl<T: ToOwned + ?Sized + 'static> IntoStatic for Cow<'_, T> {
43 type Static = Cow<'static, T>;
44
45 fn into_static(self) -> Self::Static {
46 Self::Static::Owned(self.into_owned())
47 }
48}
49
50impl<T: IntoStatic> IntoStatic for Option<T> {
51 type Static = Option<T::Static>;
52
53 fn into_static(self) -> Self::Static {
54 self.map(IntoStatic::into_static)
55 }
56}
57
58impl<T: IntoStatic, E: IntoStatic> IntoStatic for Result<T, E> {
59 type Static = Result<T::Static, E::Static>;
60
61 fn into_static(self) -> Self::Static {
62 self.map(IntoStatic::into_static)
63 .map_err(IntoStatic::into_static)
64 }
65}
66
67impl<T: IntoStatic, const N: usize> IntoStatic for [T; N] {
68 type Static = [T::Static; N];
69
70 fn into_static(self) -> Self::Static {
71 self.map(IntoStatic::into_static)
72 }
73}
74
75#[cfg(any(feature = "alloc", feature = "std"))]
76impl<T: IntoStatic> IntoStatic for Vec<T> {
77 type Static = Vec<T::Static>;
78
79 fn into_static(self) -> Self::Static {
80 self.into_iter().map(IntoStatic::into_static).collect()
81 }
82}
83
84macro_rules! impl_tuple {
85 ($($name: ident: $type: ident),+) => {
86 impl<$($type: $crate::IntoStatic),+> $crate::IntoStatic for ($($type,)+) {
87 type Static = ($($type::Static,)+);
88
89 fn into_static(self) -> Self::Static {
90 let ($($name,)+) = self;
91 ($($name.into_static(),)+)
92 }
93 }
94 }
95}
96
97impl_tuple!(a: A);
98impl_tuple!(a: A, b: B);
99impl_tuple!(a: A, b: B, c: C);
100impl_tuple!(a: A, b: B, c: C, d: D);
101impl_tuple!(a: A, b: B, c: C, d: D, e: E);
102impl_tuple!(a: A, b: B, c: C, d: D, e: E, f: F);
103impl_tuple!(a: A, b: B, c: C, d: D, e: E, f: F, g: G);
104impl_tuple!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H);
105impl_tuple!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I);
106impl_tuple!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J);
107impl_tuple!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K);
108impl_tuple!(a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J, k: K, l: L);