|
| 1 | +use std::fmt::{Debug, Display}; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use crate::export::user_data::{ArcData, Map, MapMut, MapOwned}; |
| 5 | +use crate::export::NativeClass; |
| 6 | +use crate::object::ownership::Shared; |
| 7 | + |
| 8 | +use super::{Instance, TInstance}; |
| 9 | + |
| 10 | +/// Trait for types that can be used as the `self` or `#[self]` argument (receiver) for methods |
| 11 | +/// exported through the `#[methods]` attribute macro. This trait has no public interface, and |
| 12 | +/// is not intended to be implemented by users. |
| 13 | +/// |
| 14 | +/// Notably, this is implemented for [`Instance`] and [`TInstance`] along with the usual `self` |
| 15 | +/// reference types. For types using [`ArcData`] as the wrapper specifically, [`Arc<C>`] is also |
| 16 | +/// allowed. |
| 17 | +/// |
| 18 | +/// The trait is unsealed for technical coherence issues, but is not intended to be implemented |
| 19 | +/// by users. Changes to the definition of this trait are not considered breaking changes under |
| 20 | +/// semver. |
| 21 | +pub trait Receiver<C: NativeClass>: Sized { |
| 22 | + #[doc(hidden)] |
| 23 | + type This<'a>; |
| 24 | + #[doc(hidden)] |
| 25 | + type Err: std::error::Error; |
| 26 | + |
| 27 | + #[doc(hidden)] |
| 28 | + fn with_instance<F, R>(instance: TInstance<'_, C, Shared>, f: F) -> Result<R, Self::Err> |
| 29 | + where |
| 30 | + F: for<'a> FnOnce(Self::This<'a>) -> R; |
| 31 | +} |
| 32 | + |
| 33 | +/// Error type indicating that an operation can't fail. |
| 34 | +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 35 | +#[allow(clippy::exhaustive_enums)] // explicitly uninhabited |
| 36 | +pub enum Infallible {} |
| 37 | + |
| 38 | +impl std::error::Error for Infallible {} |
| 39 | +impl Display for Infallible { |
| 40 | + fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 41 | + unreachable!("uninhabited enum") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<'r, C: NativeClass> Receiver<C> for TInstance<'r, C, Shared> { |
| 46 | + type This<'a> = TInstance<'a, C, Shared>; |
| 47 | + type Err = Infallible; |
| 48 | + |
| 49 | + #[inline] |
| 50 | + fn with_instance<F, R>(instance: TInstance<'_, C, Shared>, f: F) -> Result<R, Self::Err> |
| 51 | + where |
| 52 | + F: for<'a> FnOnce(Self::This<'a>) -> R, |
| 53 | + { |
| 54 | + Ok(f(instance)) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<C: NativeClass> Receiver<C> for Instance<C, Shared> { |
| 59 | + type This<'a> = Instance<C, Shared>; |
| 60 | + type Err = Infallible; |
| 61 | + |
| 62 | + #[inline] |
| 63 | + fn with_instance<F, R>(instance: TInstance<'_, C, Shared>, f: F) -> Result<R, Self::Err> |
| 64 | + where |
| 65 | + F: for<'a> FnOnce(Self::This<'a>) -> R, |
| 66 | + { |
| 67 | + Ok(f(instance.claim())) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<'r, C: NativeClass> Receiver<C> for &'r C |
| 72 | +where |
| 73 | + C::UserData: Map, |
| 74 | +{ |
| 75 | + type This<'a> = &'a C; |
| 76 | + type Err = <C::UserData as Map>::Err; |
| 77 | + |
| 78 | + #[inline] |
| 79 | + fn with_instance<F, R>(instance: TInstance<'_, C, Shared>, f: F) -> Result<R, Self::Err> |
| 80 | + where |
| 81 | + F: for<'a> FnOnce(Self::This<'a>) -> R, |
| 82 | + { |
| 83 | + instance.map(|this, _| f(this)) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<'r, C: NativeClass> Receiver<C> for &'r mut C |
| 88 | +where |
| 89 | + C::UserData: MapMut, |
| 90 | +{ |
| 91 | + type This<'a> = &'a mut C; |
| 92 | + type Err = <C::UserData as MapMut>::Err; |
| 93 | + |
| 94 | + #[inline] |
| 95 | + fn with_instance<F, R>(instance: TInstance<'_, C, Shared>, f: F) -> Result<R, Self::Err> |
| 96 | + where |
| 97 | + F: for<'a> FnOnce(Self::This<'a>) -> R, |
| 98 | + { |
| 99 | + instance.map_mut(|this, _| f(this)) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<C: NativeClass> Receiver<C> for C |
| 104 | +where |
| 105 | + C::UserData: MapOwned, |
| 106 | +{ |
| 107 | + type This<'a> = C; |
| 108 | + type Err = <C::UserData as MapOwned>::Err; |
| 109 | + |
| 110 | + #[inline] |
| 111 | + fn with_instance<F, R>(instance: TInstance<'_, C, Shared>, f: F) -> Result<R, Self::Err> |
| 112 | + where |
| 113 | + F: for<'a> FnOnce(Self::This<'a>) -> R, |
| 114 | + { |
| 115 | + instance.map_owned(|this, _| f(this)) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<C> Receiver<C> for Arc<C> |
| 120 | +where |
| 121 | + C: NativeClass<UserData = ArcData<C>>, |
| 122 | +{ |
| 123 | + type This<'a> = Arc<C>; |
| 124 | + type Err = Infallible; |
| 125 | + |
| 126 | + #[inline] |
| 127 | + fn with_instance<F, R>(instance: TInstance<'_, C, Shared>, f: F) -> Result<R, Self::Err> |
| 128 | + where |
| 129 | + F: for<'a> FnOnce(Self::This<'a>) -> R, |
| 130 | + { |
| 131 | + let (_, script) = instance.claim().decouple(); |
| 132 | + Ok(f(script.into_inner())) |
| 133 | + } |
| 134 | +} |
0 commit comments