lin_state/
resource.rs

1/// A resource is a type that can be moved inside or outside of a guard.
2pub trait Resource {
3    /// Clone the resource state. The function is unsafe, because
4    /// only one instance of the resource state should exist at a time.
5    /// If there are multiple instances of the resource state, we need to make
6    /// sure that their states are synchronized. If one instance is dropped
7    /// and cleans up the resource, the other instances should become invalid.
8    unsafe fn clone_state(&self) -> Self;
9    /// Set weather the resource should be cleaned up when it is dropped.
10    /// If have multiple states of the same resource, we need to make sure
11    /// that only one of them is set to clean up the resource.
12    ///
13    /// This method is unsafe, because it is possible to disable the cleanup
14    /// of a resource, which brings the resource into an invalid state.
15    unsafe fn set_cleanup_enabled(&mut self, cleanup_enabled: bool);
16}
17
18// Implement the Resource trait for tuples of resources.
19macro_rules! tuple_impls {
20    ( $( $name:ident )+ ) => {
21        impl <$($name: Resource),+> Resource for ($($name,)+) {
22            unsafe fn clone_state(&self) -> Self {
23                #[allow(nonstandard_style)]
24                let ($($name,)+) = self;
25                ($($name.clone_state(),)+)
26            }
27            unsafe fn set_cleanup_enabled(&mut self, cleanup_enabled: bool) {
28                #[allow(nonstandard_style)]
29                let ($($name,)+) = self;
30                $($name.set_cleanup_enabled(cleanup_enabled);)+
31            }
32        }
33    };
34}
35
36tuple_impls! { A }
37tuple_impls! { A B }
38tuple_impls! { A B C }
39tuple_impls! { A B C D }
40tuple_impls! { A B C D E }
41tuple_impls! { A B C D E F }
42tuple_impls! { A B C D E F G }
43tuple_impls! { A B C D E F G H }
44tuple_impls! { A B C D E F G H I }
45tuple_impls! { A B C D E F G H I J }
46tuple_impls! { A B C D E F G H I J K }
47tuple_impls! { A B C D E F G H I J K L }
48
49// Implement the Resource trait for empty tuple.
50impl Resource for () {
51    unsafe fn clone_state(&self) -> Self {}
52    unsafe fn set_cleanup_enabled(&mut self, _cleanup_enabled: bool) {}
53}