1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#[cfg(feature = "abi")]
pub use unc_abi::__private::ChunkedAbiEntry;
#[cfg(feature = "abi")]
pub use unc_abi::{
    AbiBorshParameter, AbiFunction, AbiFunctionKind, AbiFunctionModifier, AbiJsonParameter,
    AbiParameters, AbiType,
};
#[cfg(feature = "abi")]
mod result_type_ext;

#[cfg(feature = "abi")]
pub use result_type_ext::ResultTypeExt;

use crate::IntoStorageKey;
use borsh::{to_vec, BorshSerialize};

/// Converts a Borsh serializable object into a `Vec<u8>` that is used for a storage key.
///
/// [`BorshStorageKey`](crate::BorshStorageKey) should be used instead of implementing
/// this manually.
///
/// ```
/// use unc_sdk::borsh::BorshSerialize;
/// use unc_sdk::BorshStorageKey;
/// use unc_sdk::collections::LookupMap;
///
/// #[derive(BorshSerialize, BorshStorageKey)]
///  enum StorageKey {
///     FungibleToken,
///     Metadata { sub_key: String },
/// }
///
/// let lookup_map_1: LookupMap<u64, String> = LookupMap::new(StorageKey::Metadata { sub_key: String::from("yo") });
/// let lookup_map_2: LookupMap<String, String> = LookupMap::new(StorageKey::FungibleToken);
/// ```
pub trait BorshIntoStorageKey: BorshSerialize {}

impl<T> IntoStorageKey for T
where
    T: BorshIntoStorageKey,
{
    fn into_storage_key(self) -> Vec<u8> {
        to_vec(&self).unwrap()
    }
}