@@ -10,7 +10,14 @@ use crate::{
10
10
prelude:: * ,
11
11
types:: { ForeignOwnable , Opaque , ScopeGuard } ,
12
12
} ;
13
- use core:: { ffi:: c_ulong, marker:: PhantomData , mem, ops:: Deref , ptr:: NonNull } ;
13
+ use core:: {
14
+ ffi:: c_ulong,
15
+ marker:: PhantomData ,
16
+ mem,
17
+ ops:: { Deref , DerefMut } ,
18
+ pin:: Pin ,
19
+ ptr:: NonNull ,
20
+ } ;
14
21
15
22
/// Flags passed to `XArray::new` to configure the `XArray`.
16
23
type Flags = bindings:: gfp_t ;
@@ -75,6 +82,20 @@ impl<'a, T: ForeignOwnable> Guard<'a, T> {
75
82
// (`self.0`).
76
83
unsafe { T :: borrow ( self . 0 . as_ptr ( ) . cast ( ) ) }
77
84
}
85
+
86
+ /// Mutably borrow the underlying value wrapped by the `Guard`.
87
+ ///
88
+ /// Returns a `T::BorrowedMut` type for the owned `ForeignOwnable` type.
89
+ pub fn borrow_mut ( & mut self ) -> T :: BorrowedMut < ' _ > {
90
+ // SAFETY: `self.0` was produced by a call to `into_foreign` when it was
91
+ // inserted into the `XArray`. Because obtaining a `Guard` for the
92
+ // underlying value requires taking a lock, this is the only guard in
93
+ // existence for the value pointed to by `self.0`. Becuase of the
94
+ // existence of `&'a mut self` and the transfer of the lifetime `'a` to
95
+ // the return type of this function, the value returned by `borrow_mut`
96
+ // cannot overlap with other calls to `borrow` or `borrow_mut`.
97
+ unsafe { T :: borrow_mut ( self . 0 . as_ptr ( ) as _ ) }
98
+ }
78
99
}
79
100
80
101
// Convenience impl for `ForeignOwnable` types whose `Borrowe
8000
d`
@@ -91,6 +112,20 @@ where
91
112
}
92
113
}
93
114
115
+ // Convenience impl for `ForeignOwnable` types whose `Borrowed` form implements
116
+ // `DerefMut`
117
+ impl < ' a , T : ForeignOwnable > DerefMut for Guard < ' a , T >
118
+ where
119
+ T :: Borrowed < ' a > : Deref ,
120
+ T :: BorrowedMut < ' a > : DerefMut ,
121
+ for < ' b > T :: Borrowed < ' b > : Into < & ' b <T :: Borrowed < ' a > as Deref >:: Target > ,
122
+ for < ' b > T :: BorrowedMut < ' b > : Into < & ' b mut <T :: Borrowed < ' a > as Deref >:: Target > ,
123
+ {
124
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
125
+ self . borrow_mut ( ) . into ( )
126
+ }
127
+ }
128
+
94
129
impl < ' a , T : ForeignOwnable > Drop for Guard < ' a , T > {
95
130
fn drop ( & mut self ) {
96
131
// SAFETY: By the type invariant, we own the XArray lock, so we must
0 commit comments