1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! `#[derive(HopperInitSpace)]` - standalone derive that emits
//! `const INIT_SPACE: usize` equal to `size_of::<Self>()`.
//!
//! Mirror of Anchor's `#[derive(InitSpace)]` with the Hopper naming
//! prefix (derive names share the same global namespace as item names
//! in some attribute positions, so a prefix avoids clashing with
//! Anchor's derive when both are in scope during a migration).
//!
//! For structs declared through `#[hopper::state]`, this value is
//! already emitted as part of the state's layout constants. The
//! standalone derive is for hand-authored `#[repr(C)]` structs that
//! want to participate in Hopper's `space =` idiom without adopting
//! the full layout attribute:
//!
//! ```ignore
//! use hopper::prelude::*;
//!
//! #[derive(HopperInitSpace)]
//! #[repr(C)]
//! pub struct Registration {
//! pub bump: u8,
//! pub delegate: [u8; 32],
//! }
//!
//! // Generated:
//! // impl Registration {
//! // pub const INIT_SPACE: usize = core::mem::size_of::<Self>();
//! // }
//! //
//! // Then use at the call site:
//! // #[account(init, payer = authority, space = 16 + Registration::INIT_SPACE)]
//! // pub registration: Account<'info, Registration>,
//! ```
//!
//! The `16 +` offset accounts for Hopper's versioned header; the
//! derive only reports the body size. Programs that skip Hopper's
//! header (the raw-Pod path) use `Registration::INIT_SPACE` directly.
//!
//! ## Why `size_of` and not field-by-field summation
//!
//! Anchor's derive walks fields because its wire format is Borsh,
//! which packs variable-length types (`Vec<T>`, `Option<T>`, `String`)
//! with dynamic framing. Hopper's zero-copy layouts are
//! `#[repr(C)]`-stable alignment-1 byte runs with no variable framing;
//! `size_of::<Self>()` is exactly the wire size. A field-walking
//! implementation would be strictly less correct for Hopper's case
//! because it would miss trailing padding that Rust inserts when the
//! user declares an unusual layout (which `#[repr(C)]` + alignment-1
//! fields already prevents, but the derive should not assume).
use TokenStream;
use quote;
use ;