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
use std::fmt;
use std::hash::{Hash, Hasher};
use std::cmp::Ordering;
use std::marker::PhantomData;

use super::Asset;


pub struct Handle<A>
    where A: Asset,
{
    id: u32,
    _marker: PhantomData<A>,
}

unsafe impl<A> Send for Handle<A>
    where A: Asset,
{}
unsafe impl<A> Sync for Handle<A>
    where A: Asset,
{}

impl<A> fmt::Debug for Handle<A>
    where A: Asset,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.id)
    }
}

impl<A> fmt::Display for Handle<A>
    where A: Asset,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.id)
    }
}

impl<A> Hash for Handle<A>
    where A: Asset,
{
    #[inline(always)]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl<A> PartialOrd for Handle<A>
    where A: Asset,
{
    #[inline(always)]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.id.partial_cmp(&other.id)
    }
}

impl<A> Ord for Handle<A>
    where A: Asset,
{
    #[inline(always)]
    fn cmp(&self, other: &Self) -> Ordering {
        self.id.cmp(&other.id)
    }
}

impl<A> PartialEq for Handle<A>
    where A: Asset,
{
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        self.id.eq(&other.id)
    }
}

impl<A> Eq for Handle<A>
    where A: Asset,
{}

impl<A> Clone for Handle<A>
    where A: Asset,
{
    #[inline(always)]
    fn clone(&self) -> Self {
        Handle {
            id: self.id,
            _marker: PhantomData,
        }
    }
}

impl<A> Copy for Handle<A>
    where A: Asset,
{}

impl<A> Handle<A>
    where A: Asset,
{
    #[inline(always)]
    pub(crate) fn new(id: u32) -> Self {
        Handle {
            id: id,
            _marker: PhantomData,
        }
    }

    #[inline(always)]
    pub fn id(&self) -> u32 {
        self.id
    }
}