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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! Unified trait interface for tree operations across different backends
//!
//! This module provides a uniform API for CRUD operations on netabase store trees,
//! abstracting away the differences between SledStore, RedbStore, and IndexedDBStore.
use crateNetabaseError;
/// Synchronous tree operations trait for native backends (SledStore, RedbStore).
///
/// `NetabaseTreeSync` provides a unified interface for CRUD operations on a specific model type,
/// allowing you to write backend-agnostic code that works with both SledStore and RedbStore.
///
/// All methods are synchronous and designed for native (non-WASM) environments.
///
/// # Type Parameters
///
/// * `'db` - Lifetime parameter that ties the tree to its parent database, ensuring trees cannot outlive their stores
/// * `D` - The definition type (generated by `#[netabase_definition_module]`)
/// * `M` - The model type (annotated with `#[derive(NetabaseModel)]`)
///
/// # Implementations
///
/// This trait is implemented by:
/// - `SledStoreTree<'db, D, M>` - High-performance embedded database
/// - `RedbStoreTree<'db, D, M>` - ACID-compliant embedded database
///
/// # Examples
///
/// ## Writing Backend-Agnostic Code
///
/// ```
/// use netabase_store::{netabase_definition_module, NetabaseModel, netabase};
/// use netabase_store::databases::sled_store::SledStore;
/// use netabase_store::traits::tree::NetabaseTreeSync;
/// use netabase_store::error::NetabaseError;
///
/// #[netabase_definition_module(MyDefinition, MyKeys)]
/// mod my_models {
/// use super::*;
/// use netabase_store::{NetabaseModel, netabase};
///
/// #[derive(NetabaseModel, Clone, Debug, PartialEq,
/// bincode::Encode, bincode::Decode,
/// serde::Serialize, serde::Deserialize)]
/// #[netabase(MyDefinition)]
/// pub struct User {
/// #[primary_key]
/// pub id: u64,
/// pub name: String,
/// }
/// }
/// use my_models::*;
///
/// // This function works with ANY backend
/// fn count_users<'a, T>(tree: &T) -> Result<usize, NetabaseError>
/// where
/// T: NetabaseTreeSync<'a, MyDefinition, User>
/// {
/// tree.len()
/// }
///
/// // Use with Sled
/// let sled_store = SledStore::<MyDefinition>::temp().unwrap();
/// let sled_tree = sled_store.open_tree::<User>();
/// let count1 = count_users(&sled_tree).unwrap();
/// assert_eq!(count1, 0);
/// ```
///
/// # See Also
///
/// - [`crate::databases::sled_store::SledStoreTree`] - Sled implementation
/// - [`crate::databases::redb_store::RedbStoreTree`] - Redb implementation
/// Asynchronous tree operations trait for WASM backends (IndexedDBStore)
///
/// This trait provides a uniform interface for CRUD operations on a specific model type.
/// All methods are asynchronous and suitable for WASM environments.
///
/// The generic parameters D and M are not bounded here to avoid duplicating complex trait bounds.
/// Implementors should ensure proper bounds are specified in their impl blocks.