structecs 0.4.0

A structural data access framework. Type-safe extraction from nested structures with Arc-based smart pointers.
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use std::{
    fmt::Debug,
    ops::Deref,
    ptr::NonNull,
    sync::{Arc, Weak},
};

use crate::{Extractable, entity::EntityData};

/// A smart pointer to a component that keeps the entity data alive.
///
/// `Acquirable<T>` provides transparent access to component `T` through `Deref`,
/// while maintaining ownership of the underlying entity data via reference counting.
///
/// # Thread Safety
///
/// `Acquirable<T>` implements `Send` and `Sync` when `T: Send + Sync`, allowing
/// safe sharing across threads. The internal reference counting is handled by `Arc`,
/// which provides thread-safe access to immutable data.
///
/// # Examples
///
/// ```
/// use structecs::*;
///
/// #[derive(Extractable)]
/// struct Player {
///     name: String,
///     health: u32,
/// }
///
/// let player = Acquirable::new(Player {
///     name: "Alice".to_string(),
///     health: 100,
/// });
///
/// // Access via Deref
/// assert_eq!(player.name, "Alice");
/// ```
pub struct Acquirable<T: Extractable> {
    target: NonNull<T>,
    pub(crate) inner: Arc<EntityData>,
}

/// A weak reference to an entity's component.
///
/// `WeakAcquirable<T>` does not keep the entity alive and must be upgraded
/// to an `Acquirable<T>` to access the component data.
///
/// This is useful for preventing circular references and implementing
/// cache-like structures.
///
/// # Thread Safety
///
/// `WeakAcquirable<T>` implements `Send` and `Sync` when `T: Send + Sync`,
/// allowing weak references to be safely shared and transferred across threads.
///
/// # Examples
///
/// ```
/// use structecs::*;
///
/// #[derive(Extractable)]
/// struct Entity {
///     id: u32,
/// }
///
/// let entity = Acquirable::new(Entity { id: 42 });
/// let weak = entity.downgrade();
///
/// // Entity is still alive
/// assert!(weak.upgrade().is_some());
///
/// drop(entity);
///
/// // Entity has been dropped
/// assert!(weak.upgrade().is_none());
/// ```
pub struct WeakAcquirable<T: Extractable> {
    inner: Weak<EntityData>,
    _marker: std::marker::PhantomData<T>,
}

impl<T: Extractable> Acquirable<T> {
    pub fn new(target: T) -> Self {
        let data = Arc::new(EntityData::new(target, crate::get_extractor::<T>()));
        Acquirable::new_raw(data.data.cast(), data)
    }

    /// Create an `Acquirable<T>` from a value of type `U` that contains `T`.
    ///
    /// This is a compile-time checked version that validates the type relationship
    /// between `U` and `T` at compile time (in debug builds).
    ///
    /// # Compile-time Guarantees
    ///
    /// In debug builds, this function includes a compile-time check that ensures
    /// `U` contains `T` as an extractable component. The check is based on type
    /// metadata generated by the `#[derive(Extractable)]` macro.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Entity { id: u32 }
    ///
    /// #[derive(Extractable)]
    /// #[extractable(entity)]
    /// struct Player {
    ///     entity: Entity,
    ///     name: String
    /// }
    ///
    /// let player_data = Player {
    ///     entity: Entity { id: 42 },
    ///     name: "Steve".to_string(),
    /// };
    ///
    /// // Create Acquirable<Entity> from Player
    /// let entity: Acquirable<Entity> = Acquirable::new_checked(player_data);
    /// assert_eq!(entity.id, 42);
    /// ```
    ///
    /// # Panics
    ///
    /// In debug builds, panics at compile-time if `U` does not contain `T`
    /// as an extractable component.
    pub fn new_checked<U: Extractable>(target: U) -> Acquirable<T> {
        #[cfg(debug_assertions)]
        const {
            if !crate::ExtractionMetadata::is_has::<U, T>() {
                panic!("Type U must contain T as extractable component")
            }
        }
        let data = Arc::new(EntityData::new(target, crate::get_extractor::<U>()));
        // SAFETY: unwrap_unchecked is safe here because:
        // 1. The compile-time check above (in debug builds) ensures U contains T in its metadata
        // 2. extract_ptr returns None only when the type is not found in the metadata
        // 3. Since U contains T by construction (verified at compile-time), extract_ptr<T>()
        //    will always return Some(ptr)
        // 4. In release builds, incorrect usage is caught by the debug build tests
        let extracted = unsafe { data.extract_ptr::<T>().unwrap_unchecked() };
        Acquirable::new_raw(extracted, data)
    }

    #[inline(always)]
    pub(crate) fn new_raw(target: NonNull<T>, inner: Arc<EntityData>) -> Self {
        Self { target, inner }
    }

    /// Extract a component with compile-time type relationship checking.
    ///
    /// This is a compile-time checked version of [`extract`](Self::extract) that
    /// validates the type relationship at compile time (in debug builds) and panics
    /// instead of returning `None`.
    ///
    /// # Compile-time Guarantees
    ///
    /// In debug builds, this function includes a compile-time check that ensures
    /// `T` contains `U` as an extractable component. The check is based on type
    /// metadata generated by the `#[derive(Extractable)]` macro.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Health { value: u32 }
    ///
    /// #[derive(Extractable)]
    /// #[extractable(health)]
    /// struct Player {
    ///     name: String,
    ///     health: Health,
    /// }
    ///
    /// let player = Acquirable::new(Player {
    ///     name: "Alice".to_string(),
    ///     health: Health { value: 100 },
    /// });
    ///
    /// // Compile-time checked extraction
    /// let health: Acquirable<Health> = player.extract_checked::<Health>();
    /// assert_eq!(health.value, 100);
    ///
    /// // This would cause a compile-time panic in debug builds:
    /// // let other = player.extract_checked::<OtherType>();
    /// ```
    ///
    /// # Panics
    ///
    /// In debug builds, panics at compile-time if `T` does not contain `U`
    /// as an extractable component.
    ///
    /// # See Also
    ///
    /// - [`extract`](Self::extract) - Returns `Option<Acquirable<U>>` for runtime checking
    pub fn extract_checked<U: Extractable>(&self) -> Acquirable<U> {
        #[cfg(debug_assertions)]
        const {
            if !crate::ExtractionMetadata::is_has::<T, U>() {
                panic!("Type T must contain U as extractable component")
            }
        }
        // SAFETY: unwrap_unchecked is safe here because:
        // 1. The compile-time check above (in debug builds) ensures T contains U in its metadata
        // 2. The EntityData was created with an Extractor that knows about all extractable types
        // 3. extract_ptr returns None only when the type is not in the metadata
        // 4. Since T contains U (verified at compile-time), extract_ptr<U>() always returns Some(ptr)
        // 5. The pointer is valid as long as `self.inner` is alive, which is guaranteed by Arc
        let extracted = unsafe { self.inner.extract_ptr::<U>().unwrap_unchecked() };
        Acquirable::new_raw(extracted, self.inner.clone())
    }

    /// Extract a different component type from the same entity.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Health {
    ///     value: u32,
    /// }
    ///
    /// #[derive(Extractable)]
    /// #[extractable(health)]
    /// struct Player {
    ///     name: String,
    ///     health: Health,
    /// }
    ///
    /// let player = Acquirable::new(Player {
    ///     name: "Alice".to_string(),
    ///     health: Health { value: 100 },
    /// });
    ///
    /// let health = player.extract::<Health>().unwrap();
    /// assert_eq!(health.value, 100);
    /// ```
    #[inline(always)]
    pub fn extract<U: Extractable>(&self) -> Option<Acquirable<U>> {
        // SAFETY: extract_ptr performs type checking via the Extractor
        // and only returns a pointer if type U exists in the entity.
        let extracted = unsafe { self.inner.extract_ptr::<U>()? };
        Some(Acquirable::new_raw(extracted, self.inner.clone()))
    }

    /// Create a weak reference to this entity's component.
    ///
    /// The weak reference does not keep the entity alive and can be upgraded
    /// back to an `Acquirable` if the entity still exists.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Entity {
    ///     id: u32,
    /// }
    ///
    /// let entity = Acquirable::new(Entity { id: 42 });
    /// let weak = entity.downgrade();
    ///
    /// assert!(weak.upgrade().is_some());
    /// ```
    #[inline(always)]
    pub fn downgrade(&self) -> WeakAcquirable<T> {
        WeakAcquirable {
            inner: Arc::downgrade(&self.inner),
            _marker: std::marker::PhantomData,
        }
    }

    /// Check if two `Acquirable` pointers point to the same entity data.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Entity {
    ///     id: u32,
    /// }
    ///
    /// let entity1 = Acquirable::new(Entity { id: 42 });
    /// let entity2 = entity1.clone();
    /// let entity3 = Acquirable::new(Entity { id: 42 });
    ///
    /// assert!(entity1.ptr_eq(&entity2));
    /// assert!(!entity1.ptr_eq(&entity3));
    /// ```
    #[inline(always)]
    pub fn ptr_eq<U: Extractable>(&self, other: &Acquirable<U>) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }

    /// Get the number of strong references to the entity data.
    ///
    /// This is only available in debug builds for debugging purposes.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Entity {
    ///     id: u32,
    /// }
    ///
    /// let entity = Acquirable::new(Entity { id: 42 });
    ///
    /// #[cfg(debug_assertions)]
    /// {
    ///     assert_eq!(entity.strong_count(), 1);
    ///     let entity2 = entity.clone();
    ///     assert_eq!(entity.strong_count(), 2);
    /// }
    /// ```
    #[cfg(debug_assertions)]
    #[inline(always)]
    pub fn strong_count(&self) -> usize {
        Arc::strong_count(&self.inner)
    }

    /// Get the number of weak references to the entity data.
    ///
    /// This is only available in debug builds for debugging purposes.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Entity {
    ///     id: u32,
    /// }
    ///
    /// let entity = Acquirable::new(Entity { id: 42 });
    ///
    /// #[cfg(debug_assertions)]
    /// {
    ///     assert_eq!(entity.weak_count(), 0);
    ///     let weak = entity.downgrade();
    ///     assert_eq!(entity.weak_count(), 1);
    /// }
    /// ```
    #[cfg(debug_assertions)]
    #[inline(always)]
    pub fn weak_count(&self) -> usize {
        Arc::weak_count(&self.inner)
    }
}

impl<T: Extractable> WeakAcquirable<T> {
    /// Upgrade the weak reference to an `Acquirable` if the entity is still alive.
    ///
    /// Returns `None` if the entity has been dropped.
    ///
    /// # Examples
    ///
    /// ```
    /// use structecs::*;
    ///
    /// #[derive(Extractable)]
    /// struct Entity {
    ///     id: u32,
    /// }
    ///
    /// let entity = Acquirable::new(Entity { id: 42 });
    /// let weak = entity.downgrade();
    ///
    /// // Entity is still alive
    /// assert!(weak.upgrade().is_some());
    ///
    /// drop(entity);
    ///
    /// // Entity has been dropped
    /// assert!(weak.upgrade().is_none());
    /// ```
    #[inline(always)]
    pub fn upgrade(&self) -> Option<Acquirable<T>> {
        let inner = self.inner.upgrade()?;
        // SAFETY: unwrap_unchecked is safe here because:
        // 1. WeakAcquirable<T> was created from an Acquirable<T> via downgrade()
        // 2. The original Acquirable<T> was created with EntityData that contains type T
        // 3. EntityData is immutable after creation - its type structure never changes
        // 4. Therefore, if upgrade() succeeds (Arc is still alive), extract_ptr<T>()
        //    will always find T in the same location it was originally stored
        // 5. The returned pointer is valid for the lifetime of the upgraded Arc
        Some(Acquirable::new_raw(
            unsafe { inner.extract_ptr::<T>().unwrap_unchecked() },
            inner,
        ))
    }
}

impl<T: Extractable> Clone for Acquirable<T> {
    #[inline(always)]
    fn clone(&self) -> Self {
        Self {
            target: self.target,
            inner: self.inner.clone(),
        }
    }
}

impl<T: Extractable> Deref for Acquirable<T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        unsafe { self.target.as_ref() }
    }
}

impl<T: Extractable + Debug> Debug for Acquirable<T> {
    #[inline(always)]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Acquirable")
            .field("target", &**self)
            .finish()
    }
}

// SAFETY: Acquirable<T> can be safely sent between threads if T: Send + Sync.
//
// Thread-safety guarantees:
// - The `target` field is a NonNull<T> pointer that points into EntityData's heap allocation.
//   The data is immutable after creation (no interior mutability), so shared references
//   are safe across threads when T: Sync.
// - The `inner` field is an Arc<EntityData>, which provides thread-safe reference counting.
//   Arc already implements Send when T: Send + Sync.
// - Since T is accessed only through shared references (via Deref), we require T: Sync.
// - We also require T: Send because the underlying data may be moved between threads
//   when the last Arc is dropped on a different thread than where it was created.
unsafe impl<T: Extractable + Send + Sync> Send for Acquirable<T> {}
unsafe impl<T: Extractable + Send + Sync> Sync for Acquirable<T> {}

impl<T: Extractable> Clone for WeakAcquirable<T> {
    #[inline(always)]
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            _marker: std::marker::PhantomData,
        }
    }
}

// SAFETY: WeakAcquirable<T> can be safely sent between threads if T: Send + Sync.
//
// Thread-safety guarantees:
// - The `inner` field is a Weak<EntityData>, which provides thread-safe weak reference counting.
//   Weak already implements Send when T: Send + Sync.
// - WeakAcquirable does not directly hold any data; it only holds a weak reference.
//   When upgraded to Acquirable<T>, the same Send + Sync bounds apply.
// - The PhantomData<T> marker is zero-sized and does not affect thread safety.
// - We require T: Send + Sync for the same reasons as Acquirable<T>: the underlying
//   data must be safely transferable and shareable across threads.
unsafe impl<T: Extractable + Send + Sync> Send for WeakAcquirable<T> {}
unsafe impl<T: Extractable + Send + Sync> Sync for WeakAcquirable<T> {}