@@ -110,7 +110,7 @@ impl Keyring {
110
110
where
111
111
D : AsRef < str > ,
112
112
{
113
- Keyring :: new_impl ( 0 ) . request_keyring ( description)
113
+ Keyring :: new_impl ( 0 ) . request_keyring ( description. as_ref ( ) )
114
114
}
115
115
116
116
/// Requests a keyring with the given description by searching the thread, process, and session
@@ -120,7 +120,7 @@ impl Keyring {
120
120
/// the key.
121
121
pub fn request_with_fallback < D , I > ( description : D , info : I ) -> Result < Self >
122
122
where
123
- D : AsRef < str > ,
123
+ D : Borrow < <keytypes :: Keyring as KeyType > :: Description > ,
124
124
I : AsRef < str > ,
125
125
{
126
126
Keyring :: new_impl ( 0 ) . request_keyring_with_fallback ( description, info)
@@ -197,8 +197,11 @@ impl Keyring {
197
197
check_call ( unsafe { keyctl_unlink ( keyring. id , self . id ) } , ( ) )
198
198
}
199
199
200
- fn search_impl ( & self , type_ : & str , description : & str ) -> Result < libc:: c_long > {
201
- let type_cstr = CString :: new ( type_) . unwrap ( ) ;
200
+ fn search_impl < K > ( & self , description : & str ) -> Result < libc:: c_long >
201
+ where
202
+ K : KeyType ,
203
+ {
204
+ let type_cstr = CString :: new ( K :: name ( ) ) . unwrap ( ) ;
202
205
let desc_cstr = CString :: new ( description) . unwrap ( ) ;
203
206
check_call_ret ( unsafe {
204
207
keyctl_search ( self . id , type_cstr. as_ptr ( ) , desc_cstr. as_ptr ( ) , self . id )
@@ -210,11 +213,12 @@ impl Keyring {
210
213
/// If it is found, it is attached to the keyring (if `write` permission to the keyring and
211
214
/// `link` permission on the key exist) and return it. Requires the `search` permission on the
212
215
/// keyring. Any children keyrings without the `search` permission are ignored.
213
- pub fn search_for_key < D > ( & self , description : D ) -> Result < Key >
216
+ pub fn search_for_key < K , D > ( & self , description : D ) -> Result < Key >
214
217
where
215
- D : AsRef < str > ,
218
+ K : KeyType ,
219
+ D : Borrow < K :: Description > ,
216
220
{
217
- let res = self . search_impl ( "user" , description. as_ref ( ) ) ?;
221
+ let res = self . search_impl :: < K > ( & description. borrow ( ) . description ( ) ) ?;
218
222
check_call ( res, Key :: new_impl ( res as key_serial_t ) )
219
223
}
220
224
@@ -226,9 +230,9 @@ impl Keyring {
226
230
/// ignored.
227
231
pub fn search_for_keyring < D > ( & self , description : D ) -> Result < Self >
228
232
where
229
- D : AsRef < str > ,
233
+ D : Borrow < <keytypes :: Keyring as KeyType > :: Description > ,
230
234
{
231
- let res = self . search_impl ( "keyring" , description. as_ref ( ) ) ?;
235
+ let res = self . search_impl :: < keytypes :: Keyring > ( & description. borrow ( ) . description ( ) ) ?;
232
236
check_call ( res, Keyring :: new_impl ( res as key_serial_t ) )
233
237
}
234
238
@@ -313,8 +317,11 @@ impl Keyring {
313
317
Ok ( Keyring :: new_impl ( key. id ) )
314
318
}
315
319
316
- fn request_impl ( & self , type_ : & str , description : & str ) -> Result < KeyringSerial > {
317
- let type_cstr = CString :: new ( type_) . unwrap ( ) ;
320
+ fn request_impl < K > ( & self , description : & str ) -> Result < KeyringSerial >
321
+ where
322
+ K : KeyType ,
323
+ {
324
+ let type_cstr = CString :: new ( K :: name ( ) ) . unwrap ( ) ;
318
325
let desc_cstr = CString :: new ( description) . unwrap ( ) ;
319
326
check_call_ret_serial ( unsafe {
320
327
request_key ( type_cstr. as_ptr ( ) , desc_cstr. as_ptr ( ) , ptr:: null ( ) , self . id )
@@ -325,11 +332,12 @@ impl Keyring {
325
332
/// keyrings.
326
333
///
327
334
/// If it is found, it is attached to the keyring.
328
- pub fn request_key < D > ( & self , description : D ) -> Result < Key >
335
+ pub fn request_key < K , D > ( & self , description : D ) -> Result < Key >
329
336
where
330
- D : AsRef < str > ,
337
+ K : KeyType ,
338
+ D : Borrow < K :: Description > ,
331
339
{
332
- let res = self . request_impl ( "user" , description. as_ref ( ) ) ?;
340
+ let res = self . request_impl :: < K > ( & description. borrow ( ) . description ( ) ) ?;
333
341
check_call ( i64:: from ( res) , Key :: new_impl ( res) )
334
342
}
335
343
@@ -339,19 +347,17 @@ impl Keyring {
339
347
/// If it is found, it is attached to the keyring.
340
348
pub fn request_keyring < D > ( & self , description : D ) -> Result < Self >
341
349
where
342
- D : AsRef < str > ,
350
+ D : Borrow < <keytypes :: Keyring as KeyType > :: Description > ,
343
351
{
344
- let res = self . request_impl ( "keyring" , description. as_ref ( ) ) ?;
352
+ let res = self . request_impl :: < keytypes :: Keyring > ( & description. borrow ( ) . description ( ) ) ?;
345
353
check_call ( i64:: from ( res) , Keyring :: new_impl ( res) )
346
354
}
347
355
348
- fn request_fallback_impl (
349
- & self ,
350
- type_ : & str ,
351
- description : & str ,
352
- info : & str ,
353
- ) -> Result < KeyringSerial > {
354
- let type_cstr = CString :: new ( type_) . unwrap ( ) ;
356
+ fn request_fallback_impl < K > ( & self , description : & str , info : & str ) -> Result < KeyringSerial >
357
+ where
358
+ K : KeyType ,
359
+ {
360
+ let type_cstr = CString :: new ( K :: name ( ) ) . unwrap ( ) ;
355
361
let desc_cstr = CString :: new ( description) . unwrap ( ) ;
356
362
let info_cstr = CString :: new ( info) . unwrap ( ) ;
357
363
check_call_ret_serial ( unsafe {
@@ -370,12 +376,14 @@ impl Keyring {
370
376
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
371
377
/// the key. If found, it will be attached to the current keyring. Requires `write` permission
372
378
/// to the keyring.
373
- pub fn request_key_with_fallback < D , I > ( & self , description : D , info : I ) -> Result < Key >
379
+ pub fn request_key_with_fallback < K , D , I > ( & self , description : D , info : I ) -> Result < Key >
374
380
where
375
- D : AsRef < str > ,
381
+ K : KeyType ,
382
+ D : Borrow < K :: Description > ,
376
383
I : AsRef < str > ,
377
384
{
378
- let res = self . request_fallback_impl ( "user" , description. as_ref ( ) , info. as_ref ( ) ) ?;
385
+ let res =
386
+ self . request_fallback_impl :: < K > ( & description. borrow ( ) . description ( ) , info. as_ref ( ) ) ?;
379
387
check_call ( i64:: from ( res) , Key :: new_impl ( res) )
380
388
}
381
389
@@ -387,10 +395,13 @@ impl Keyring {
387
395
/// to the keyring.
388
396
pub fn request_keyring_with_fallback < D , I > ( & self , description : D , info : I ) -> Result < Self >
389
397
where
390
- D : AsRef < str > ,
398
+ D : Borrow < <keytypes :: Keyring as KeyType > :: Description > ,
391
399
I : AsRef < str > ,
392
400
{
393
- let res = self . request_fallback_impl ( "keyring" , description. as_ref ( ) , info. as_ref ( ) ) ?;
401
+ let res = self . request_fallback_impl :: < keytypes:: Keyring > (
402
+ & description. borrow ( ) . description ( ) ,
403
+ info. as_ref ( ) ,
404
+ ) ?;
394
405
check_call ( i64:: from ( res) , Keyring :: new_impl ( res) )
395
406
}
396
407
@@ -514,24 +525,26 @@ impl Key {
514
525
515
526
/// Requests a key with the given description by searching the thread, process, and session
516
527
/// keyrings.
517
- pub fn request < D > ( description : D ) -> Result < Self >
528
+ pub fn request < K , D > ( description : D ) -> Result < Self >
518
529
where
519
- D : AsRef < str > ,
530
+ K : KeyType ,
531
+ D : Borrow < K :: Description > ,
520
532
{
521
- Keyring :: new_impl ( 0 ) . request_key ( description)
533
+ Keyring :: new_impl ( 0 ) . request_key :: < K , _ > ( description)
522
534
}
523
535
524
536
/// Requests a key with the given description by searching the thread, process, and session
525
537
/// keyrings.
526
538
///
527
539
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
528
540
/// the key.
529
- pub fn request_with_fallback < D , I > ( description : D , info : I ) -> Result < Self >
541
+ pub fn request_with_fallback < K , D , I > ( description : D , info : I ) -> Result < Self >
530
542
where
531
- D : AsRef < str > ,
543
+ K : KeyType ,
544
+ D : Borrow < K :: Description > ,
532
545
I : AsRef < str > ,
533
546
{
534
- Keyring :: new_impl ( 0 ) . request_key_with_fallback ( description, info)
547
+ Keyring :: new_impl ( 0 ) . request_key_with_fallback :: < K , _ , _ > ( description, info)
535
548
}
536
549
537
550
/// Update the payload in the key.
@@ -995,7 +1008,9 @@ mod tests {
995
1008
. add_key :: < keytypes:: User , _ , _ > ( description, payload. as_bytes ( ) )
996
1009
. unwrap ( ) ;
997
1010
998
- let found_key = keyring. request_key ( description) . unwrap ( ) ;
1011
+ let found_key = keyring
1012
+ . request_key :: < keytypes:: User , _ > ( description)
1013
+ . unwrap ( ) ;
999
1014
assert_eq ! ( found_key, key) ;
1000
1015
1001
1016
// Clean up.
@@ -1038,7 +1053,9 @@ mod tests {
1038
1053
. add_key :: < keytypes:: User , _ , _ > ( description, payload. as_bytes ( ) )
1039
1054
. unwrap ( ) ;
1040
1055
1041
- let found_key = keyring. search_for_key ( description) . unwrap ( ) ;
1056
+ let found_key = keyring
1057
+ . search_for_key :: < keytypes:: User , _ > ( description)
1058
+ . unwrap ( ) ;
1042
1059
assert_eq ! ( found_key, key) ;
1043
1060
1044
1061
// Clean up.
0 commit comments