gemachain_program/
lib.rs

1#![allow(incomplete_features)]
2#![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(specialization))]
3#![cfg_attr(RUSTC_NEEDS_PROC_MACRO_HYGIENE, feature(proc_macro_hygiene))]
4
5// Allows macro expansion of `use ::gemachain_program::*` to work within this crate
6extern crate self as gemachain_program;
7
8pub mod account_info;
9pub mod blake3;
10pub mod borsh;
11pub mod bpf_loader;
12pub mod bpf_loader_deprecated;
13pub mod bpf_loader_upgradeable;
14pub mod clock;
15pub mod decode_error;
16pub mod ed25519_program;
17pub mod entrypoint;
18pub mod entrypoint_deprecated;
19pub mod epoch_schedule;
20pub mod feature;
21pub mod fee_calculator;
22pub mod hash;
23pub mod incinerator;
24pub mod instruction;
25pub mod keccak;
26pub mod carats;
27pub mod loader_instruction;
28pub mod loader_upgradeable_instruction;
29pub mod log;
30pub mod message;
31pub mod native_token;
32pub mod nonce;
33pub mod program;
34pub mod program_error;
35pub mod program_memory;
36pub mod program_option;
37pub mod program_pack;
38pub mod program_stubs;
39pub mod pubkey;
40pub mod rent;
41pub mod sanitize;
42pub mod secp256k1_program;
43pub mod secp256k1_recover;
44pub mod serialize_utils;
45pub mod short_vec;
46pub mod slot_hashes;
47pub mod slot_history;
48pub mod stake;
49pub mod stake_history;
50pub mod system_instruction;
51pub mod system_program;
52pub mod sysvar;
53
54pub mod config {
55    pub mod program {
56        crate::declare_id!("Config1111111111111111111111111111111111111");
57    }
58}
59
60pub mod vote {
61    pub mod program {
62        crate::declare_id!("Vote111111111111111111111111111111111111111");
63    }
64}
65
66/// Same as `declare_id` except report that this id has been deprecated
67pub use gemachain_sdk_macro::program_declare_deprecated_id as declare_deprecated_id;
68/// Convenience macro to declare a static public key and functions to interact with it
69///
70/// Input: a single literal base58 string representation of a program's id
71///
72/// # Example
73///
74/// ```
75/// # // wrapper is used so that the macro invocation occurs in the item position
76/// # // rather than in the statement position which isn't allowed.
77/// use std::str::FromStr;
78/// use gemachain_program::{declare_id, pubkey::Pubkey};
79///
80/// # mod item_wrapper {
81/// #   use gemachain_program::declare_id;
82/// declare_id!("My11111111111111111111111111111111111111111");
83/// # }
84/// # use item_wrapper::id;
85///
86/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
87/// assert_eq!(id(), my_id);
88/// ```
89pub use gemachain_sdk_macro::program_declare_id as declare_id;
90
91#[macro_use]
92extern crate serde_derive;
93
94#[macro_use]
95extern crate gemachain_frozen_abi_macro;
96
97/// Convenience macro for doing integer division where the opersation's safety
98/// can be checked at compile-time
99///
100/// Since `unchecked_div_by_const!()` is supposed to fail at compile-time, abuse
101/// doctests to cover failure modes
102/// Literal denominator div-by-zero fails
103/// ```compile_fail
104/// # use gemachain_program::unchecked_div_by_const;
105/// # fn main() {
106/// # let _ = unchecked_div_by_const!(10, 0);
107/// # }
108/// ```
109/// #
110/// # Const denominator div-by-zero fails
111/// ```compile_fail
112/// # use gemachain_program::unchecked_div_by_const;
113/// # fn main() {
114/// # const D: u64 = 0;
115/// # let _ = unchecked_div_by_const!(10, D);
116/// # }
117/// ```
118/// #
119/// # Non-const denominator fails
120/// ```compile_fail
121/// # use gemachain_program::unchecked_div_by_const;
122/// # fn main() {
123/// # let d = 0;
124/// # let _ = unchecked_div_by_const!(10, d);
125/// # }
126/// ```
127/// #
128/// Literal denominator div-by-zero fails
129/// ```compile_fail
130/// # use gemachain_program::unchecked_div_by_const;
131/// # fn main() {
132/// # const N: u64 = 10;
133/// # let _ = unchecked_div_by_const!(N, 0);
134/// # }
135/// ```
136/// #
137/// # Const denominator div-by-zero fails
138/// ```compile_fail
139/// # use gemachain_program::unchecked_div_by_const;
140/// # fn main() {
141/// # const N: u64 = 10;
142/// # const D: u64 = 0;
143/// # let _ = unchecked_div_by_const!(N, D);
144/// # }
145/// ```
146/// #
147/// # Non-const denominator fails
148/// ```compile_fail
149/// # use gemachain_program::unchecked_div_by_const;
150/// # fn main() {
151/// # const N: u64 = 10;
152/// # let d = 0;
153/// # let _ = unchecked_div_by_const!(N, d);
154/// # }
155/// ```
156/// #
157/// Literal denominator div-by-zero fails
158/// ```compile_fail
159/// # use gemachain_program::unchecked_div_by_const;
160/// # fn main() {
161/// # let n = 10;
162/// # let _ = unchecked_div_by_const!(n, 0);
163/// # }
164/// ```
165/// #
166/// # Const denominator div-by-zero fails
167/// ```compile_fail
168/// # use gemachain_program::unchecked_div_by_const;
169/// # fn main() {
170/// # let n = 10;
171/// # const D: u64 = 0;
172/// # let _ = unchecked_div_by_const!(n, D);
173/// # }
174/// ```
175/// #
176/// # Non-const denominator fails
177/// ```compile_fail
178/// # use gemachain_program::unchecked_div_by_const;
179/// # fn main() {
180/// # let n = 10;
181/// # let d = 0;
182/// # let _ = unchecked_div_by_const!(n, d);
183/// # }
184/// ```
185/// #
186#[macro_export]
187macro_rules! unchecked_div_by_const {
188    ($num:expr, $den:expr) => {{
189        // Ensure the denominator is compile-time constant
190        let _ = [(); ($den - $den) as usize];
191        // Compile-time constant integer div-by-zero passes for some reason
192        // when invoked from a compilation unit other than that where this
193        // macro is defined. Do an explicit zero-check for now. Sorry about the
194        // ugly error messages!
195        // https://users.rust-lang.org/t/unexpected-behavior-of-compile-time-integer-div-by-zero-check-in-declarative-macro/56718
196        let _ = [(); ($den as usize) - 1];
197        #[allow(clippy::integer_arithmetic)]
198        let quotient = $num / $den;
199        quotient
200    }};
201}
202
203#[cfg(test)]
204mod tests {
205    use super::unchecked_div_by_const;
206
207    #[test]
208    fn test_unchecked_div_by_const() {
209        const D: u64 = 2;
210        const N: u64 = 10;
211        let n = 10;
212        assert_eq!(unchecked_div_by_const!(10, 2), 5);
213        assert_eq!(unchecked_div_by_const!(N, 2), 5);
214        assert_eq!(unchecked_div_by_const!(n, 2), 5);
215        assert_eq!(unchecked_div_by_const!(10, D), 5);
216        assert_eq!(unchecked_div_by_const!(N, D), 5);
217        assert_eq!(unchecked_div_by_const!(n, D), 5);
218    }
219}