Skip to main content

slop_alloc/backend/
mod.rs

1mod cpu;
2mod io;
3
4use std::{borrow::Cow, fmt::Debug, rc::Rc, sync::Arc};
5
6pub use cpu::*;
7pub use io::*;
8
9use crate::{
10    mem::{CopyError, DeviceMemory},
11    Allocator,
12};
13
14/// # Safety
15///
16/// TODO
17pub unsafe trait Backend:
18    Sized + Allocator + DeviceMemory + Clone + Debug + Send + Sync + 'static
19{
20    fn copy_from<B, T>(&self, data: T) -> Result<T::Output, CopyError>
21    where
22        B: Backend,
23        T: HasBackend + CopyIntoBackend<Self, B>,
24    {
25        data.copy_into_backend(self)
26    }
27}
28
29pub trait GlobalBackend: Backend + 'static {
30    fn global() -> &'static Self;
31}
32
33pub trait HasBackend {
34    type Backend: Backend;
35
36    fn backend(&self) -> &Self::Backend;
37}
38
39impl<T> HasBackend for &T
40where
41    T: HasBackend,
42{
43    type Backend = T::Backend;
44
45    fn backend(&self) -> &Self::Backend {
46        (**self).backend()
47    }
48}
49
50impl<T> HasBackend for &mut T
51where
52    T: HasBackend,
53{
54    type Backend = T::Backend;
55
56    fn backend(&self) -> &Self::Backend {
57        (**self).backend()
58    }
59}
60
61impl<'a, T> HasBackend for Cow<'a, T>
62where
63    T: HasBackend + Clone,
64{
65    type Backend = T::Backend;
66
67    fn backend(&self) -> &Self::Backend {
68        self.as_ref().backend()
69    }
70}
71
72impl<T> HasBackend for Box<T>
73where
74    T: HasBackend,
75{
76    type Backend = T::Backend;
77
78    fn backend(&self) -> &Self::Backend {
79        self.as_ref().backend()
80    }
81}
82
83impl<T> HasBackend for Arc<T>
84where
85    T: HasBackend,
86{
87    type Backend = T::Backend;
88
89    fn backend(&self) -> &Self::Backend {
90        self.as_ref().backend()
91    }
92}
93
94impl<T> HasBackend for Rc<T>
95where
96    T: HasBackend,
97{
98    type Backend = T::Backend;
99
100    fn backend(&self) -> &Self::Backend {
101        self.as_ref().backend()
102    }
103}