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 all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ neardev

.idea
.vscode
**/.DS_Store
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Fixed gas assertion in `*_transfer_call` implementations of FT and NFT standards to only require what's needed. [PR 760](https://github.com/near/near-sdk-rs/pull/760)
- Fixed events being emitted in FT standard to include refund transfers and burn events. [PR 752](https://github.com/near/near-sdk-rs/pull/752)
- Moved `VMContext` to a local type defined in SDK to avoid duplicate types. [PR 785](https://github.com/near/near-sdk-rs/pull/785)
- Moved `Metadata` and `MethodMetadata` to a pseudo-private module as these are just types used within macros and not stable. [PR 771](https://github.com/near/near-sdk-rs/pull/771)

### Removed
- Remove `Clone` implementation for `Promise` (error prone) https://github.com/near/near-sdk-rs/pull/783
Expand Down
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
Binary file removed near-sdk/src/.DS_Store
Binary file not shown.
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()
}
}