1use alloy_sol_types::SolValue;
18use core::marker::PhantomData;
19use ink_primitives::{
20 reflect::{
21 AbiDecodeWith,
22 ScaleEncoding,
23 SolEncoding,
24 },
25 MessageResult,
26};
27use scale::{
28 Decode,
29 DecodeAll,
30};
31
32#[derive(Debug)]
36pub struct ReturnType<T>(PhantomData<fn() -> T>);
37
38impl<T> Clone for ReturnType<T> {
39 #[inline]
40 fn clone(&self) -> Self {
41 *self
42 }
43}
44
45impl<T> Copy for ReturnType<T> {}
46
47impl<T> Default for ReturnType<T> {
48 #[inline]
49 fn default() -> Self {
50 Self(Default::default())
51 }
52}
53
54#[derive(Debug, Copy, Clone)]
56pub struct Set<T>(pub T);
57
58impl<T> Set<T> {
59 #[inline]
61 pub fn value(self) -> T {
62 self.0
63 }
64}
65
66#[derive(Debug)]
68pub struct Unset<T>(PhantomData<fn() -> T>);
69
70impl<T> Clone for Unset<T> {
71 #[inline]
72 fn clone(&self) -> Self {
73 *self
74 }
75}
76
77impl<T> Copy for Unset<T> {}
78
79impl<T> Default for Unset<T> {
80 #[inline]
81 fn default() -> Self {
82 Self(Default::default())
83 }
84}
85
86pub trait Unwrap {
91 type Output;
93
94 fn unwrap_or_else<F>(self, f: F) -> Self::Output
96 where
97 F: FnOnce() -> Self::Output;
98}
99
100impl<T> Unwrap for Unset<T> {
101 type Output = T;
102
103 #[inline]
104 fn unwrap_or_else<F>(self, f: F) -> Self::Output
105 where
106 F: FnOnce() -> Self::Output,
107 {
108 f()
109 }
110}
111
112impl<T> Unwrap for Set<T> {
113 type Output = T;
114
115 #[inline]
116 fn unwrap_or_else<F>(self, _: F) -> Self::Output
117 where
118 F: FnOnce() -> Self::Output,
119 {
120 self.value()
121 }
122}
123
124pub trait DecodeMessageResult<Abi>: Sized {
128 fn decode_output(buffer: &[u8]) -> crate::Result<MessageResult<Self>>;
131}
132
133impl<R> DecodeMessageResult<ScaleEncoding> for R
134where
135 R: Decode,
136 MessageResult<R>: Decode,
137{
138 fn decode_output(mut buffer: &[u8]) -> crate::Result<MessageResult<Self>> {
139 let decoded = MessageResult::<R>::decode_all(&mut buffer)?;
140 Ok(decoded)
141 }
142}
143
144impl<R> DecodeMessageResult<SolEncoding> for R
145where
146 R: SolValue + From<<<R as SolValue>::SolType as alloy_sol_types::SolType>::RustType>,
147{
148 fn decode_output(buffer: &[u8]) -> crate::Result<MessageResult<Self>> {
149 let decoded = R::decode_with(buffer)?;
152 Ok(Ok(decoded))
153 }
154}