Mercurial > crates > nonstick
comparison src/module.rs @ 12:30831c70e5c0
Remove PhantomData usage.
PhantomData is used through the library to substitute associated
constants (to types). However, calling
`PamItem::item_type(PhantomData<T>)` can easily be substituted by
calling `T::item_type()`, getting rid of the need for PhantomData.
author | Marc Brinkmann <git@marcbrinkmann.de> |
---|---|
date | Sun, 26 Feb 2017 12:12:36 +0100 |
parents | 74b53b921b23 |
children | cc39d168aeb8 |
comparison
equal
deleted
inserted
replaced
11:827faa554528 | 12:30831c70e5c0 |
---|---|
1 //! Functions for use in pam modules. | 1 //! Functions for use in pam modules. |
2 | 2 |
3 use libc::c_char; | 3 use libc::c_char; |
4 use std::{mem, ptr}; | 4 use std::{mem, ptr}; |
5 use std::ffi::{CStr, CString}; | 5 use std::ffi::{CStr, CString}; |
6 use std::marker::PhantomData; | |
7 | 6 |
8 use constants; | 7 use constants; |
9 use constants::*; | 8 use constants::*; |
10 | 9 |
11 /// Opaque type, used as a pointer when making pam API calls. | 10 /// Opaque type, used as a pointer when making pam API calls. |
64 /// | 63 /// |
65 /// For example, the type PamConv maps to the constant PAM_CONV. The pam | 64 /// For example, the type PamConv maps to the constant PAM_CONV. The pam |
66 /// API contract specifies that when the API function `pam_get_item` is | 65 /// API contract specifies that when the API function `pam_get_item` is |
67 /// called with the constant PAM_CONV, it will return a value of type | 66 /// called with the constant PAM_CONV, it will return a value of type |
68 /// `PamConv`. | 67 /// `PamConv`. |
69 fn item_type(_: PhantomData<Self>) -> PamItemType; | 68 fn item_type() -> PamItemType; |
70 } | 69 } |
71 | 70 |
72 /// Gets some value, identified by `key`, that has been set by the module | 71 /// Gets some value, identified by `key`, that has been set by the module |
73 /// previously. | 72 /// previously. |
74 /// | 73 /// |
119 /// See `pam_get_item` in | 118 /// See `pam_get_item` in |
120 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html | 119 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html |
121 pub fn get_item<'a, T: PamItem>(pamh: &'a PamHandleT) -> PamResult<&'a T> { | 120 pub fn get_item<'a, T: PamItem>(pamh: &'a PamHandleT) -> PamResult<&'a T> { |
122 let mut ptr: *const PamItemT = ptr::null(); | 121 let mut ptr: *const PamItemT = ptr::null(); |
123 let (res, item) = unsafe { | 122 let (res, item) = unsafe { |
124 let r = pam_get_item(pamh, PamItem::item_type(PhantomData::<T>), &mut ptr); | 123 let r = pam_get_item(pamh, T::item_type(), &mut ptr); |
125 let typed_ptr: *const T = mem::transmute(ptr); | 124 let typed_ptr: *const T = mem::transmute(ptr); |
126 let t: &T = &*typed_ptr; | 125 let t: &T = &*typed_ptr; |
127 (r, t) | 126 (r, t) |
128 }; | 127 }; |
129 if constants::PAM_SUCCESS == res { | 128 if constants::PAM_SUCCESS == res { |
143 pub fn set_item_str<'a, P: PamItem>(pamh: &'a mut PamHandleT, item: &str) -> PamResult<()> { | 142 pub fn set_item_str<'a, P: PamItem>(pamh: &'a mut PamHandleT, item: &str) -> PamResult<()> { |
144 let c_item = CString::new(item).unwrap().as_ptr(); | 143 let c_item = CString::new(item).unwrap().as_ptr(); |
145 | 144 |
146 let res = unsafe { | 145 let res = unsafe { |
147 pam_set_item(pamh, | 146 pam_set_item(pamh, |
148 PamItem::item_type(PhantomData::<P>), | 147 P::item_type(), |
149 | 148 |
150 // unwrapping is okay here, as c_item will not be a NULL | 149 // unwrapping is okay here, as c_item will not be a NULL |
151 // pointer | 150 // pointer |
152 (c_item as *const PamItemT).as_ref().unwrap()) | 151 (c_item as *const PamItemT).as_ref().unwrap()) |
153 }; | 152 }; |