Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: move macro generated types to pseudo-private module #771

Merged
merged 8 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/fungible-token/res/fungible_token.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/non_fungible_token.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl ImplItemMethodInfo {
/// ```
/// will produce this struct:
/// ```ignore
/// near_sdk::MethodMetadata {
/// near_sdk::__private::MethodMetadata {
/// name: "f3".to_string(),
/// is_view: false,
/// is_init: false,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl ImplItemMethodInfo {
};

quote! {
near_sdk::MethodMetadata {
near_sdk::__private::MethodMetadata {
name: #method_name_str.to_string(),
is_view: #is_view,
is_init: #is_init,
Expand Down
10 changes: 5 additions & 5 deletions near-sdk-macros/src/core_impl/metadata/metadata_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl MetadataVisitor {
pub extern "C" fn metadata() {
#panic_hook
use borsh::*;
let metadata = near_sdk::Metadata::new(vec![
let metadata = near_sdk::__private::Metadata::new(vec![
#(#methods),*
]);
let data = near_sdk::borsh::BorshSerialize::try_to_vec(&metadata).expect("Failed to serialize the metadata using Borsh");
Expand Down Expand Up @@ -100,8 +100,8 @@ mod tests {
pub extern "C" fn metadata() {
near_sdk::env::setup_panic_hook();
use borsh::*;
let metadata = near_sdk::Metadata::new(vec![
near_sdk::MethodMetadata {
let metadata = near_sdk::__private::Metadata::new(vec![
near_sdk::__private::MethodMetadata {
name: "f1".to_string(),
is_view: true,
is_init: false,
Expand All @@ -110,7 +110,7 @@ mod tests {
callbacks_vec: None,
result: None
},
near_sdk::MethodMetadata {
near_sdk::__private::MethodMetadata {
name: "f2".to_string(),
is_view: false,
is_init: false,
Expand All @@ -129,7 +129,7 @@ mod tests {
callbacks_vec: None,
result: None
},
near_sdk::MethodMetadata {
near_sdk::__private::MethodMetadata {
name: "f3".to_string(),
is_view: false,
is_init: false,
Expand Down
2 changes: 1 addition & 1 deletion near-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn borsh_storage_key(item: TokenStream) -> TokenStream {
);
};
TokenStream::from(quote! {
impl near_sdk::BorshIntoStorageKey for #name {}
impl near_sdk::__private::BorshIntoStorageKey for #name {}
})
}

Expand Down
8 changes: 5 additions & 3 deletions near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ pub use near_sys as sys;
mod promise;
pub use promise::{Promise, PromiseOrValue};

mod metadata;
pub use metadata::{Metadata, MethodMetadata};
// Private types just used within macro generation, not stable to be used.
#[doc(hidden)]
#[path = "private/mod.rs"]
pub mod __private;

pub mod json_types;

Expand All @@ -41,7 +43,7 @@ pub use near_vm_logic::VMConfig;
pub use test_utils::context::VMContext;

pub mod utils;
pub use crate::utils::storage_key_impl::*;
pub use crate::utils::storage_key_impl::IntoStorageKey;
pub use crate::utils::*;

#[cfg(not(target_arch = "wasm32"))]
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions near-sdk/src/private/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod metadata;

pub use metadata::{Metadata, MethodMetadata};

use crate::IntoStorageKey;
use borsh::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 near_sdk::borsh::BorshSerialize;
/// use near_sdk::BorshStorageKey;
/// use near_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> {
self.try_to_vec().unwrap()
}
}
31 changes: 0 additions & 31 deletions near-sdk/src/utils/storage_key_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use borsh::BorshSerialize;

/// Converts Self into a [`Vec<u8>`] that is used for a storage key through [`into_storage_key`].
///
/// [`into_storage_key`]: IntoStorageKey::into_storage_key
Expand Down Expand Up @@ -35,32 +33,3 @@ impl IntoStorageKey for u8 {
vec![self]
}
}

/// Converts a Borsh serializable object into a `Vec<u8>` that is used for a storage key.
///
/// ```
/// use near_sdk::borsh::BorshSerialize;
/// use near_sdk::BorshIntoStorageKey;
/// use near_sdk::collections::LookupMap;
///
/// #[derive(BorshSerialize)]
/// enum StorageKey {
/// FungibleToken,
/// Metadata { sub_key: String },
/// }
///
/// impl BorshIntoStorageKey for StorageKey {}
///
/// 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> {
self.try_to_vec().unwrap()
}
}