Skip to main content

typhoon_accounts/accounts/
mutable.rs

1use {
2    crate::{
3        AccountData, FromAccountInfo, FromRaw, ReadableAccount, Signer, SignerAccount, SignerCheck,
4        WritableAccount,
5    },
6    solana_account_view::AccountView,
7    typhoon_errors::Error,
8};
9
10pub struct Mut<T: ReadableAccount>(pub(crate) T);
11
12impl<'a, T> FromAccountInfo<'a> for Mut<T>
13where
14    T: FromAccountInfo<'a> + ReadableAccount,
15{
16    #[inline(always)]
17    fn try_from_info(info: &'a AccountView) -> Result<Self, Error> {
18        Ok(Mut(T::try_from_info(info)?))
19    }
20}
21
22impl<T> AsRef<AccountView> for Mut<T>
23where
24    T: ReadableAccount,
25{
26    #[inline(always)]
27    fn as_ref(&self) -> &AccountView {
28        self.0.as_ref()
29    }
30}
31
32impl<'a, T> From<Mut<T>> for &'a AccountView
33where
34    T: ReadableAccount + Into<&'a AccountView>,
35{
36    #[inline(always)]
37    fn from(value: Mut<T>) -> Self {
38        value.0.into()
39    }
40}
41
42impl<T> ReadableAccount for Mut<T> where T: ReadableAccount {}
43impl<T> WritableAccount for Mut<T> where T: ReadableAccount {}
44
45impl<T> AccountData for Mut<T>
46where
47    T: AccountData + ReadableAccount,
48{
49    type Data = T::Data;
50}
51
52impl<T, C> SignerAccount for Mut<Signer<'_, T, C>>
53where
54    T: ReadableAccount,
55    C: SignerCheck,
56{
57}
58
59#[doc(hidden)]
60impl<'a, T> Mut<T>
61where
62    T: ReadableAccount + FromRaw<'a>,
63{
64    #[inline(always)]
65    pub fn from_raw_info(info: &'a AccountView) -> Self {
66        Mut(T::from_raw(info))
67    }
68}