alloc_checked/
claim.rs

1use alloc::rc::Rc;
2use alloc::sync::Arc;
3use core::convert::Infallible;
4use core::marker::PhantomData;
5
6/// A marker trait for infallible cloneable objects.
7/// Only implement this for your type if you can guarantee that cloning it
8/// is guaranteed not to panic.
9///
10/// For details on the idea, read the [Claiming, auto and
11/// otherwise](https://smallcultfollowing.com/babysteps/blog/2024/06/21/claim-auto-and-otherwise/)
12/// blog post.
13pub trait Claim: Clone {}
14
15// Anything which is trivially copiable is automatically infallible
16// We need to list these out since the compiler will not allow us to `impl <T: Copy> impl Claim {}`
17macro_rules! impl_claim_for {
18    ($($t:ty),*) => {
19        $(
20            impl Claim for $t {}
21        )*
22    };
23}
24
25// Generate impls for simple types
26impl_claim_for! {
27    (), u8, u16, u32, u64, u128, usize,
28    i8, i16, i32, i64, i128, isize,
29    f32, f64, bool, char
30}
31
32impl<T: ?Sized> Claim for *const T {}
33impl<T: ?Sized> Claim for *mut T {}
34impl<T: Copy, const N: usize> Claim for [T; N] {}
35impl<T: ?Sized> Claim for PhantomData<T> {}
36impl<T: ?Sized> Claim for &T {}
37
38// A few other common impls, non-exhaustive
39impl<T> Claim for Arc<T> {}
40impl<T> Claim for Rc<T> {}
41impl Claim for Infallible {}
42impl<T: Claim> Claim for Option<T> {}
43impl<T: Claim, E: Claim> Claim for Result<T, E> {}