Skip to main content

oxide_asset/
handle.rs

1//! Handle type for shared asset references
2
3use std::marker::PhantomData;
4
5/// A typed handle to an asset.
6pub struct Handle<T> {
7    /// Unique identifier for this asset.
8    pub id: u64,
9    _marker: PhantomData<T>,
10}
11
12impl<T> Clone for Handle<T> {
13    fn clone(&self) -> Self {
14        *self
15    }
16}
17
18impl<T> Copy for Handle<T> {}
19
20impl<T> std::fmt::Debug for Handle<T> {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        f.debug_struct("Handle").field("id", &self.id).finish()
23    }
24}
25
26impl<T> PartialEq for Handle<T> {
27    fn eq(&self, other: &Self) -> bool {
28        self.id == other.id
29    }
30}
31
32impl<T> Eq for Handle<T> {}
33
34impl<T> std::hash::Hash for Handle<T> {
35    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
36        self.id.hash(state);
37    }
38}
39
40impl<T> Handle<T> {
41    /// Creates a new handle with the given ID.
42    pub fn new(id: u64) -> Self {
43        Self {
44            id,
45            _marker: PhantomData,
46        }
47    }
48
49    /// Returns the raw ID.
50    pub fn id(&self) -> u64 {
51        self.id
52    }
53}
54
55/// A counter for generating unique handles.
56#[derive(Default)]
57pub struct HandleAllocator {
58    next_id: u64,
59}
60
61impl HandleAllocator {
62    /// Creates a new allocator starting from ID 0.
63    pub fn new() -> Self {
64        Self { next_id: 0 }
65    }
66
67    /// Allocates a new handle.
68    pub fn allocate<T>(&mut self) -> Handle<T> {
69        let id = self.next_id;
70        self.next_id += 1;
71        Handle::new(id)
72    }
73}