solana/sdk/src/lib.rs

130 lines
3.3 KiB
Rust
Raw Normal View History

#![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(specialization))]
#![cfg_attr(RUSTC_NEEDS_PROC_MACRO_HYGIENE, feature(proc_macro_hygiene))]
// Allows macro expansion of `use ::solana_sdk::*` to work within this crate
extern crate self as solana_sdk;
2019-09-06 15:33:58 -07:00
pub mod account;
pub mod account_utils;
pub mod bpf_loader;
pub mod bpf_loader_deprecated;
2020-08-14 12:32:45 -07:00
pub mod builtins;
pub mod clock;
pub mod commitment_config;
pub mod decode_error;
pub mod deserialize_utils;
pub mod entrypoint_native;
pub mod epoch_info;
pub mod epoch_schedule;
2019-09-06 15:33:58 -07:00
pub mod fee_calculator;
pub mod hash;
2020-04-30 22:04:08 -07:00
pub mod incinerator;
2019-09-06 15:33:58 -07:00
pub mod inflation;
pub mod instruction;
pub mod loader_instruction;
pub mod message;
pub mod native_loader;
2019-10-01 10:53:28 -07:00
pub mod native_token;
pub mod nonce;
pub mod packet;
2019-09-06 15:33:58 -07:00
pub mod poh_config;
pub mod program_option;
pub mod program_pack;
2020-02-05 12:48:30 -08:00
pub mod program_utils;
pub mod pubkey;
2019-10-30 16:25:12 -07:00
pub mod rent;
2019-09-06 15:33:58 -07:00
pub mod rpc_port;
pub mod sanitize;
pub mod secp256k1_program;
2019-09-06 15:33:58 -07:00
pub mod short_vec;
pub mod slot_hashes;
pub mod slot_history;
pub mod stake_history;
pub mod stake_weighted_timestamp;
2019-09-06 15:33:58 -07:00
pub mod system_instruction;
pub mod system_program;
pub mod sysvar;
2019-09-06 15:33:58 -07:00
pub mod timing;
/// Convenience macro to declare a static public key and functions to interact with it
///
/// Input: a single literal base58 string representation of a program's id
///
/// # Example
///
/// ```
/// # // wrapper is used so that the macro invocation occurs in the item position
/// # // rather than in the statement position which isn't allowed.
/// use std::str::FromStr;
/// use solana_sdk::{declare_id, pubkey::Pubkey};
///
/// # mod item_wrapper {
/// # use solana_sdk::declare_id;
/// declare_id!("My11111111111111111111111111111111111111111");
/// # }
/// # use item_wrapper::id;
///
/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
/// assert_eq!(id(), my_id);
/// ```
pub use solana_sdk_macro::declare_id;
pub use solana_sdk_macro::pubkeys;
Fix hygiene issues in `declare_program!` and `declare_loader!` The `declare_program!` and `declare_loader!` macros both expand to new macro definitions (based on the `$name` argument). These 'inner' macros make use of the special `$crate` metavariable to access items in the crate where the 'inner' macros is defined. However, this only works due to a bug in rustc. When a macro is expanded, all `$crate` tokens in its output are 'marked' as being resolved in the defining crate of that macro. An inner macro (including the body of its arms) is 'just' another set of tokens that appears in the body of the outer macro, so any `$crate` identifiers used there are resolved relative to the 'outer' macro. For example, consider the following code: ```rust macro_rules! outer { () => { macro_rules! inner { () => { $crate::Foo } } } } ``` The path `$crate::Foo` will be resolved relative to the crate that defines `outer`, **not** the crate which defines `inner`. However, rustc currently loses this extra resolution information (referred to as 'hygiene' information) when a crate is serialized. In the above example, this means that the macro `inner` (which gets defined in whatever crate invokes `outer!`) will behave differently depending on which crate it is invoked from: When `inner` is invoked from the same crate in which it is defined, the hygiene information will still be available, which will cause `$crate::Foo` to be resolved in the crate which defines 'outer'. When `inner` is invoked from a different crate, it will be loaded from the metadata of the crate which defines 'inner'. Since the hygiene information is currently lost, rust will 'forget' that `$crate::Foo` is supposed to be resolved in the context of 'outer'. Instead, it will be resolved relative to the crate which defines 'inner', which can cause incorrect code to compile. This bug will soon be fixed in rust (see https://github.com/rust-lang/rust/pull/72121), which will break `declare_program!` and `declare_loader!`. Fortunately, it's possible to obtain the desired behavior (`$crate` resolving in the context of the 'inner' macro) by use of a procedural macro. This commit adds a `respan!` proc-macro to the `sdk/macro` crate. Using the newly-stabilized (on Nightly) `Span::resolved_at` method, the `$crate` identifier can be made to be resolved in the context of the proper crate. Since `Span::resolved_at` is only stable on the latest nightly, referencing it on an earlier version of Rust will cause a compilation error. This requires the `rustversion` crate to be used, which allows conditionally compiling code epending on the Rust compiler version in use. Since this method is already stabilized in the latest nightly, there will never be a situation where the hygiene bug is fixed (e.g. https://github.com/rust-lang/rust/pull/72121) is merged but we are unable to call `Span::resolved_at`.
2020-06-19 22:42:11 -07:00
#[rustversion::since(1.46.0)]
pub use solana_sdk_macro::respan;
// On-chain program specific modules
pub mod account_info;
pub mod entrypoint;
pub mod entrypoint_deprecated;
pub mod log;
2020-04-28 14:33:56 -07:00
pub mod program;
pub mod program_error;
#[cfg(all(feature = "program", not(target_arch = "bpf")))]
extern crate lazy_static;
#[cfg(all(feature = "program", not(target_arch = "bpf")))]
2020-06-18 23:23:28 -07:00
pub mod program_stubs;
// Unused `solana_sdk::program_stubs!()` macro retained for source backwards compatibility with v1.3.x programs
#[macro_export]
#[deprecated(
since = "1.4.2",
note = "program_stubs macro is obsolete and can be safely removed"
)]
macro_rules! program_stubs {
() => {};
}
pub mod serialize_utils;
// Modules not usable by on-chain programs
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
pub mod client;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
pub mod genesis_config;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
2020-02-24 09:18:08 -08:00
pub mod hard_forks;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
pub mod secp256k1;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
2020-02-24 09:18:08 -08:00
pub mod shred_version;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
pub mod signature;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
pub mod signers;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
pub mod system_transaction;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
2019-09-06 15:33:58 -07:00
pub mod transaction;
2020-10-19 10:17:29 -07:00
#[cfg(feature = "everything")]
2019-09-06 15:33:58 -07:00
pub mod transport;
#[macro_use]
extern crate serde_derive;
pub extern crate bs58;
extern crate log as logger;
#[macro_use]
extern crate solana_frozen_abi_macro;