Skip to main content

dope_runtime/client/
bound.rs

1use std::{marker, mem, ops};
2
3/// Application and driver authority issued for one provider type.
4/// The exact provider object is carried by [`super::Context`]; this brand alone
5/// does not establish the ownership of an arbitrary attached value.
6#[repr(transparent)]
7pub struct Bound<'app, 'd: 'app, P: ?Sized, T = ()> {
8    value: T,
9    owner: marker::PhantomData<(*mut &'app (), *mut &'d (), *mut P)>,
10}
11
12impl<'app, 'd: 'app, P: ?Sized> Bound<'app, 'd, P> {
13    pub(in crate::client) const fn new() -> Self {
14        Self {
15            value: (),
16            owner: marker::PhantomData,
17        }
18    }
19
20    pub const fn borrow<'a>(&'a self) -> Bound<'a, 'd, P>
21    where
22        'app: 'a,
23    {
24        Bound {
25            value: (),
26            owner: marker::PhantomData,
27        }
28    }
29
30    pub const fn bind<T>(self, value: T) -> Bound<'app, 'd, P, T> {
31        Bound {
32            value,
33            owner: self.owner,
34        }
35    }
36}
37
38impl<P: ?Sized, T> ops::Deref for Bound<'_, '_, P, T> {
39    type Target = T;
40    fn deref(&self) -> &T {
41        &self.value
42    }
43}
44
45impl<P: ?Sized, T> ops::DerefMut for Bound<'_, '_, P, T> {
46    fn deref_mut(&mut self) -> &mut T {
47        &mut self.value
48    }
49}
50
51impl<P: ?Sized, T: Copy> Copy for Bound<'_, '_, P, T> {}
52impl<P: ?Sized, T: Copy> Clone for Bound<'_, '_, P, T> {
53    fn clone(&self) -> Self {
54        *self
55    }
56}
57
58const _: () = {
59    assert!(mem::size_of::<Bound<'static, 'static, ()>>() == 0);
60    assert!(mem::align_of::<Bound<'static, 'static, ()>>() == 1);
61    assert!(!mem::needs_drop::<Bound<'static, 'static, ()>>());
62    assert!(mem::size_of::<Bound<'static, 'static, (), usize>>() == mem::size_of::<usize>());
63    assert!(mem::align_of::<Bound<'static, 'static, (), usize>>() == mem::align_of::<usize>());
64    assert!(
65        mem::size_of::<Option<Bound<'static, 'static, (), std::num::NonZero<usize>>>>()
66            == mem::size_of::<usize>()
67    );
68    assert!(mem::needs_drop::<Bound<'static, 'static, (), String>>());
69};