55//! It contains a generic Rust lock and guard that allow for different backends (e.g., mutexes,
66//! spinlocks, raw spinlocks) to be provided with minimal effort.
77
8- use super :: LockClassKey ;
8+ use super :: { lockdep :: caller_lock_class , LockClassKey } ;
99use crate :: { init:: PinInit , pin_init, str:: CStr , try_pin_init, types:: Opaque , types:: ScopeGuard } ;
1010use core:: { cell:: UnsafeCell , marker:: PhantomData , marker:: PhantomPinned } ;
1111use macros:: pin_data;
@@ -114,7 +114,40 @@ unsafe impl<T: ?Sized + Send, B: Backend> Sync for Lock<T, B> {}
114114
115115impl < T , B : Backend > Lock < T , B > {
116116 /// Constructs a new lock initialiser.
117- pub fn new ( t : T , name : & ' static CStr , key : LockClassKey ) -> impl PinInit < Self > {
117+ #[ track_caller]
118+ pub fn new ( t : T ) -> impl PinInit < Self > {
119+ let ( key, name) = caller_lock_class ( ) ;
120+ Self :: new_with_key ( t, name, key)
121+ }
122+
123+ /// Constructs a new lock initialiser taking an initialiser/
124+ pub fn pin_init < E > ( t : impl PinInit < T , E > ) -> impl PinInit < Self , E >
125+ where
126+ E : core:: convert:: From < core:: convert:: Infallible > ,
127+ {
128+ let ( key, name) = caller_lock_class ( ) ;
129+ Self :: pin_init_with_key ( t, name, key)
130+ }
131+
132+ /// Constructs a new lock initialiser.
133+ #[ allow( clippy:: new_ret_no_self) ]
134+ #[ track_caller]
135+ pub fn new_named ( t : T , name : & ' static CStr ) -> impl PinInit < Self > {
136+ let ( key, _) = caller_lock_class ( ) ;
137+ Self :: new_with_key ( t, name, key)
138+ }
139+
140+ /// Constructs a new lock initialiser taking an initialiser/
141+ pub fn pin_init_named < E > ( t : impl PinInit < T , E > , name : & ' static CStr ) -> impl PinInit < Self , E >
142+ where
143+ E : core:: convert:: From < core:: convert:: Infallible > ,
144+ {
145+ let ( key, _) = caller_lock_class ( ) ;
146+ Self :: pin_init_with_key ( t, name, key)
147+ }
148+
149+ /// Constructs a new lock initialiser given a particular name and lock class key.
150+ pub fn new_with_key ( t : T , name : & ' static CStr , key : LockClassKey ) -> impl PinInit < Self > {
118151 pin_init ! ( Self {
119152 data: UnsafeCell :: new( t) ,
120153 _pin: PhantomPinned ,
@@ -126,8 +159,9 @@ impl<T, B: Backend> Lock<T, B> {
126159 } )
127160 }
128161
129- /// Constructs a new lock initialiser taking an initialiser.
130- pub fn pin_init < E > (
162+ /// Constructs a new lock initialiser taking an initialiser given a particular
163+ /// name and lock class key.
164+ pub fn pin_init_with_key < E > (
131165 t : impl PinInit < T , E > ,
132166 name : & ' static CStr ,
133167 key : LockClassKey ,
0 commit comments