comparison src/module.rs @ 7:9380392b9a60

Changes type marker parameter type from Option<T> to PhantomData<T>
author Jesse Hallett <jesse@galois.com>
date Fri, 03 Apr 2015 23:33:20 -0700
parents 2ec97116d72c
children a83c56216e21
comparison
equal deleted inserted replaced
6:2ec97116d72c 7:9380392b9a60
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};
6 7
7 use constants; 8 use constants;
8 use constants::*; 9 use constants::*;
9 10
10 /// Opaque type, used as a pointer when making pam API calls. 11 /// Opaque type, used as a pointer when making pam API calls.
64 /// 65 ///
65 /// For example, the type PamConv maps to the constant PAM_CONV. The pam 66 /// 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 67 /// 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 68 /// called with the constant PAM_CONV, it will return a value of type
68 /// `PamConv`. 69 /// `PamConv`.
69 /// 70 fn item_type(_: PhantomData<Self>) -> PamItemType;
70 /// The argument will always be `None`. Its purpose is to provide a type
71 /// label - the value is not important.
72 fn item_type(_: Option<Self>) -> PamItemType;
73 } 71 }
74 72
75 /// Gets some value, identified by `key`, that has been set by the module 73 /// Gets some value, identified by `key`, that has been set by the module
76 /// previously. 74 /// previously.
77 /// 75 ///
119 /// See `pam_get_item` in 117 /// See `pam_get_item` in
120 /// http://www.linux-pam.org/Linux-PAM-html/mwg-expected-by-module-item.html 118 /// 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> { 119 pub fn get_item<'a, T: PamItem>(pamh: &'a PamHandleT) -> PamResult<&'a T> {
122 let mut ptr: *const PamItemT = ptr::null(); 120 let mut ptr: *const PamItemT = ptr::null();
123 let (res, item) = unsafe { 121 let (res, item) = unsafe {
124 let r = pam_get_item(pamh, PamItem::item_type(None::<T>), &mut ptr); 122 let r = pam_get_item(pamh, PamItem::item_type(PhantomData::<T>), &mut ptr);
125 let typed_ptr: *const T = mem::transmute(ptr); 123 let typed_ptr: *const T = mem::transmute(ptr);
126 let t: &T = &*typed_ptr; 124 let t: &T = &*typed_ptr;
127 (r, t) 125 (r, t)
128 }; 126 };
129 if constants::PAM_SUCCESS == res { Ok(item) } else { Err(res) } 127 if constants::PAM_SUCCESS == res { Ok(item) } else { Err(res) }