Skip to content

Commit da390e7

Browse files
committed
docs: Add examples for custom derives and fixed arrays
Added comprehensive example schemas for features implemented in #50 and #51: - examples/custom_derives.lumos (170+ lines) - Demonstrates #[derive(...)] attribute usage - Shows Copy, PartialEq, Hash, Default, and other custom derives - Context-aware generation (Anchor vs pure Borsh) - Covers structs, enums, and mixed scenarios - examples/fixed_arrays.lumos (210+ lines) - Demonstrates [T; N] fixed-size array syntax - Common Solana patterns ([u8; 32] for hashes, [u8; 64] for signatures) - Nested fixed arrays - Size calculation validation Both examples include detailed comments and best practices. Related: #50 (custom derives), #51 (fixed arrays) Phase 5.3 Advanced Type System examples complete.
1 parent 14bc258 commit da390e7

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

examples/custom_derives.lumos

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Example: Custom Derive Macros in LUMOS
2+
//
3+
// This example demonstrates how to use custom derive macros
4+
// to add additional traits beyond the auto-generated ones.
5+
6+
// Example 1: Basic custom derives for equality comparison
7+
#[solana]
8+
#[derive(PartialEq, Eq, Hash)]
9+
struct PlayerStats {
10+
wins: u32,
11+
losses: u32,
12+
draws: u32,
13+
}
14+
15+
// Example 2: Custom derives with Anchor account
16+
// Note: #[account] structs get no auto-derives from Anchor,
17+
// but you can still add custom derives
18+
#[solana]
19+
#[account]
20+
#[derive(PartialEq, Eq)]
21+
struct UserAccount {
22+
wallet: PublicKey,
23+
balance: u64,
24+
verified: bool,
25+
}
26+
27+
// Example 3: Enum with custom derives
28+
#[solana]
29+
#[derive(PartialEq, Eq, Hash, Copy)]
30+
enum GameState {
31+
Lobby,
32+
InProgress,
33+
Paused,
34+
Finished,
35+
}
36+
37+
// Example 4: Complex enum with custom derives
38+
#[solana]
39+
#[derive(PartialEq, Eq)]
40+
enum GameEvent {
41+
PlayerJoined(PublicKey),
42+
ScoreUpdated {
43+
player: PublicKey,
44+
new_score: u64,
45+
},
46+
GameEnded,
47+
}
48+
49+
// Example 5: Deduplication - if you specify a derive that LUMOS
50+
// auto-generates (like Debug or Clone), it won't be duplicated
51+
#[solana]
52+
#[derive(Debug, Clone, PartialEq)] // Debug and Clone won't be duplicated
53+
struct Item {
54+
id: u64,
55+
name: String,
56+
}
57+
58+
// Generated Rust code will have:
59+
// #[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Hash)]
60+
// for PlayerStats (auto-generated + custom, deduplicated)
61+
//
62+
// #[derive(PartialEq, Eq)]
63+
// #[account]
64+
// for UserAccount (only custom derives, Anchor provides serialization)
65+
//
66+
// Benefits of custom derives:
67+
// - PartialEq, Eq: Enable equality comparisons (==, !=)
68+
// - Hash: Enable use in HashSet and HashMap
69+
// - Copy: Enable bitwise copying for small types
70+
// - Ord, PartialOrd: Enable ordering comparisons (<, >, <=, >=)

examples/fixed_arrays.lumos

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Fixed-size array examples for LUMOS
2+
// Issue #51: Add const generics support for fixed-size arrays
3+
4+
#[solana]
5+
#[account]
6+
struct FixedArrayExample {
7+
// Simple fixed array of primitives
8+
data: [u8; 32],
9+
10+
// Fixed array of Solana types
11+
authority_list: [PublicKey; 5],
12+
13+
// Nested fixed arrays (2D grid)
14+
grid: [[u8; 10]; 10],
15+
16+
// Mixed: fixed array with regular field
17+
balance: u64,
18+
inventory_slots: [PublicKey; 50],
19+
}
20+
21+
#[solana]
22+
struct Transaction {
23+
// Common Solana pattern: 32-byte hash
24+
hash: [u8; 32],
25+
26+
// Signature data
27+
signature: [u8; 64],
28+
29+
// Fixed number of signers
30+
signers: [PublicKey; 3],
31+
}
32+
33+
enum GameEvent {
34+
// Unit variant
35+
Start,
36+
37+
// Tuple variant with fixed array
38+
MultiTransfer([PublicKey; 10], u64),
39+
40+
// Struct variant with fixed array
41+
GridUpdate {
42+
position: [u16; 2],
43+
cells: [u8; 100],
44+
},
45+
}

0 commit comments

Comments
 (0)