solana_sysvar_id/
lib.rs

1//! Access to special accounts with dynamically-updated data.
2//!
3//! Sysvars are special accounts that contain dynamically-updated data about the
4//! network cluster, the blockchain history, and the executing transaction. Each
5//! sysvar is defined in its own crate. The [`clock`], [`epoch_schedule`],
6//! [`instructions`], and [`rent`] sysvars are most useful to on-chain programs.
7//!
8//! [`clock`]: https://docs.rs/solana-clock/latest
9//! [`epoch_schedule`]: https://docs.rs/solana-epoch-schedule/latest
10//! [`instructions`]: https://docs.rs/solana-program/latest/solana_program/sysvar/instructions
11//! [`rent`]: https://docs.rs/solana-rent/latest
12//!
13//! All sysvar accounts are owned by the account identified by [`solana_sysvar::ID`].
14//!
15//! [`solana_sysvar::ID`]: crate::ID
16//!
17//! For more details see the Solana [documentation on sysvars][sysvardoc].
18//!
19//! [sysvardoc]: https://docs.solanalabs.com/runtime/sysvars
20#![cfg_attr(docsrs, feature(doc_cfg))]
21#![no_std]
22
23/// Re-export types required for macros
24pub use {
25    solana_address::{declare_deprecated_id, declare_id, Address},
26    solana_sdk_ids::sysvar::{check_id, id, ID},
27};
28
29/// A type that holds sysvar data and has an associated sysvar `Address`.
30pub trait SysvarId {
31    /// The `Address` of the sysvar.
32    fn id() -> Address;
33
34    /// Returns `true` if the given address is the ID.
35    fn check_id(address: &Address) -> bool;
36}
37
38/// Implements [`SysvarId`] for a module that already uses
39/// `declare_id``
40#[macro_export]
41macro_rules! impl_sysvar_id(
42    ($type:ty) => {
43        impl $crate::SysvarId for $type {
44            fn id() -> $crate::Address {
45                id()
46            }
47
48            fn check_id(address: &$crate::Address) -> bool {
49                check_id(address)
50            }
51        }
52    }
53);
54
55/// Implements [`SysvarId`] for a module that already uses
56/// `declare_deprecated_id``
57#[macro_export]
58macro_rules! impl_deprecated_sysvar_id(
59    ($type:ty) => {
60        impl $crate::SysvarId for $type {
61            fn id() -> $crate::Address {
62                #[allow(deprecated)]
63                id()
64            }
65
66            fn check_id(address: &$crate::Address) -> bool {
67                #[allow(deprecated)]
68                check_id(address)
69            }
70        }
71    }
72);
73
74/// Declares an ID that implements [`SysvarId`].
75#[macro_export]
76macro_rules! declare_sysvar_id(
77    ($name:expr, $type:ty) => (
78        $crate::declare_id!($name);
79        $crate::impl_sysvar_id!($type);
80    )
81);
82
83/// Same as [`declare_sysvar_id`] except that it reports that this ID has been deprecated.
84#[macro_export]
85macro_rules! declare_deprecated_sysvar_id(
86    ($name:expr, $type:ty) => (
87        $crate::declare_deprecated_id!($name);
88        $crate::impl_deprecated_sysvar_id!($type);
89    )
90);