Skip to content

Commit 5e85d21

Browse files
committed
api: more trait-based changes
1 parent 7ea4e33 commit 5e85d21

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

src/api.rs

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Keyring {
110110
where
111111
D: AsRef<str>,
112112
{
113-
Keyring::new_impl(0).request_keyring(description)
113+
Keyring::new_impl(0).request_keyring(description.as_ref())
114114
}
115115

116116
/// Requests a keyring with the given description by searching the thread, process, and session
@@ -120,7 +120,7 @@ impl Keyring {
120120
/// the key.
121121
pub fn request_with_fallback<D, I>(description: D, info: I) -> Result<Self>
122122
where
123-
D: AsRef<str>,
123+
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
124124
I: AsRef<str>,
125125
{
126126
Keyring::new_impl(0).request_keyring_with_fallback(description, info)
@@ -197,8 +197,11 @@ impl Keyring {
197197
check_call(unsafe { keyctl_unlink(keyring.id, self.id) }, ())
198198
}
199199

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();
202205
let desc_cstr = CString::new(description).unwrap();
203206
check_call_ret(unsafe {
204207
keyctl_search(self.id, type_cstr.as_ptr(), desc_cstr.as_ptr(), self.id)
@@ -210,11 +213,12 @@ impl Keyring {
210213
/// If it is found, it is attached to the keyring (if `write` permission to the keyring and
211214
/// `link` permission on the key exist) and return it. Requires the `search` permission on the
212215
/// 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>
214217
where
215-
D: AsRef<str>,
218+
K: KeyType,
219+
D: Borrow<K::Description>,
216220
{
217-
let res = self.search_impl("user", description.as_ref())?;
221+
let res = self.search_impl::<K>(&description.borrow().description())?;
218222
check_call(res, Key::new_impl(res as key_serial_t))
219223
}
220224

@@ -226,9 +230,9 @@ impl Keyring {
226230
/// ignored.
227231
pub fn search_for_keyring<D>(&self, description: D) -> Result<Self>
228232
where
229-
D: AsRef<str>,
233+
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
230234
{
231-
let res = self.search_impl("keyring", description.as_ref())?;
235+
let res = self.search_impl::<keytypes::Keyring>(&description.borrow().description())?;
232236
check_call(res, Keyring::new_impl(res as key_serial_t))
233237
}
234238

@@ -313,8 +317,11 @@ impl Keyring {
313317
Ok(Keyring::new_impl(key.id))
314318
}
315319

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();
318325
let desc_cstr = CString::new(description).unwrap();
319326
check_call_ret_serial(unsafe {
320327
request_key(type_cstr.as_ptr(), desc_cstr.as_ptr(), ptr::null(), self.id)
@@ -325,11 +332,12 @@ impl Keyring {
325332
/// keyrings.
326333
///
327334
/// 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>
329336
where
330-
D: AsRef<str>,
337+
K: KeyType,
338+
D: Borrow<K::Description>,
331339
{
332-
let res = self.request_impl("user", description.as_ref())?;
340+
let res = self.request_impl::<K>(&description.borrow().description())?;
333341
check_call(i64::from(res), Key::new_impl(res))
334342
}
335343

@@ -339,19 +347,17 @@ impl Keyring {
339347
/// If it is found, it is attached to the keyring.
340348
pub fn request_keyring<D>(&self, description: D) -> Result<Self>
341349
where
342-
D: AsRef<str>,
350+
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
343351
{
344-
let res = self.request_impl("keyring", description.as_ref())?;
352+
let res = self.request_impl::<keytypes::Keyring>(&description.borrow().description())?;
345353
check_call(i64::from(res), Keyring::new_impl(res))
346354
}
347355

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();
355361
let desc_cstr = CString::new(description).unwrap();
356362
let info_cstr = CString::new(info).unwrap();
357363
check_call_ret_serial(unsafe {
@@ -370,12 +376,14 @@ impl Keyring {
370376
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
371377
/// the key. If found, it will be attached to the current keyring. Requires `write` permission
372378
/// 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>
374380
where
375-
D: AsRef<str>,
381+
K: KeyType,
382+
D: Borrow<K::Description>,
376383
I: AsRef<str>,
377384
{
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())?;
379387
check_call(i64::from(res), Key::new_impl(res))
380388
}
381389

@@ -387,10 +395,13 @@ impl Keyring {
387395
/// to the keyring.
388396
pub fn request_keyring_with_fallback<D, I>(&self, description: D, info: I) -> Result<Self>
389397
where
390-
D: AsRef<str>,
398+
D: Borrow<<keytypes::Keyring as KeyType>::Description>,
391399
I: AsRef<str>,
392400
{
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+
)?;
394405
check_call(i64::from(res), Keyring::new_impl(res))
395406
}
396407

@@ -514,24 +525,26 @@ impl Key {
514525

515526
/// Requests a key with the given description by searching the thread, process, and session
516527
/// keyrings.
517-
pub fn request<D>(description: D) -> Result<Self>
528+
pub fn request<K, D>(description: D) -> Result<Self>
518529
where
519-
D: AsRef<str>,
530+
K: KeyType,
531+
D: Borrow<K::Description>,
520532
{
521-
Keyring::new_impl(0).request_key(description)
533+
Keyring::new_impl(0).request_key::<K, _>(description)
522534
}
523535

524536
/// Requests a key with the given description by searching the thread, process, and session
525537
/// keyrings.
526538
///
527539
/// If it is not found, the `info` string will be handed off to `/sbin/request-key` to generate
528540
/// 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>
530542
where
531-
D: AsRef<str>,
543+
K: KeyType,
544+
D: Borrow<K::Description>,
532545
I: AsRef<str>,
533546
{
534-
Keyring::new_impl(0).request_key_with_fallback(description, info)
547+
Keyring::new_impl(0).request_key_with_fallback::<K, _, _>(description, info)
535548
}
536549

537550
/// Update the payload in the key.
@@ -995,7 +1008,9 @@ mod tests {
9951008
.add_key::<keytypes::User, _, _>(description, payload.as_bytes())
9961009
.unwrap();
9971010

998-
let found_key = keyring.request_key(description).unwrap();
1011+
let found_key = keyring
1012+
.request_key::<keytypes::User, _>(description)
1013+
.unwrap();
9991014
assert_eq!(found_key, key);
10001015

10011016
// Clean up.
@@ -1038,7 +1053,9 @@ mod tests {
10381053
.add_key::<keytypes::User, _, _>(description, payload.as_bytes())
10391054
.unwrap();
10401055

1041-
let found_key = keyring.search_for_key(description).unwrap();
1056+
let found_key = keyring
1057+
.search_for_key::<keytypes::User, _>(description)
1058+
.unwrap();
10421059
assert_eq!(found_key, key);
10431060

10441061
// Clean up.

0 commit comments

Comments
 (0)