Mercurial > crates > nonstick
comparison src/items.rs @ 51:9d1160b02d2c
Safety and doc fixes:
- Don't panic when given a string with a null character;
instead return `PAM_CONV_ERR`.
- Improve pattern matching and use ?s where appropriate.
- Format etc.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sat, 03 May 2025 18:41:25 -0400 |
parents | ce47901aab7a |
children |
comparison
equal
deleted
inserted
replaced
50:171fb1171053 | 51:9d1160b02d2c |
---|---|
1 use std::ffi::CStr; | |
2 | |
1 #[repr(u32)] | 3 #[repr(u32)] |
2 pub enum ItemType { | 4 pub enum ItemType { |
3 /// The service name | 5 /// The service name |
4 Service = 1, | 6 Service = 1, |
5 /// The user name | 7 /// The user name |
34 type Raw; | 36 type Raw; |
35 | 37 |
36 /// The `ItemType` for this type | 38 /// The `ItemType` for this type |
37 fn type_id() -> ItemType; | 39 fn type_id() -> ItemType; |
38 | 40 |
39 /// The function to convert from the pointer to the C-representation to this safer wrapper type | 41 /// The function to convert from the pointer to the C-representation to this safer wrapper type. |
40 /// | 42 /// |
41 /// # Safety | 43 /// # Safety |
42 /// | 44 /// |
43 /// This function can assume the pointer is a valid pointer to a `Self::Raw` instance. | 45 /// This function assumes the pointer is a valid pointer to a `Self::Raw` instance. |
44 unsafe fn from_raw(raw: *const Self::Raw) -> Self; | 46 unsafe fn from_raw(raw: *const Self::Raw) -> Self; |
45 | 47 |
46 /// The function to convert from this wrapper type to a C-compatible pointer. | 48 /// The function to convert from this wrapper type to a C-compatible pointer. |
47 fn into_raw(self) -> *const Self::Raw; | 49 fn into_raw(self) -> *const Self::Raw; |
48 } | 50 } |
49 | 51 |
50 macro_rules! cstr_item { | 52 macro_rules! cstr_item { |
51 ($name:ident) => { | 53 ($name:ident) => { |
54 ///A `CStr`-based item from a PAM conversation. | |
52 #[derive(Debug)] | 55 #[derive(Debug)] |
53 pub struct $name<'s>(pub &'s std::ffi::CStr); | 56 pub struct $name<'s>(pub &'s CStr); |
54 | 57 |
55 impl<'s> std::ops::Deref for $name<'s> { | 58 impl<'s> std::ops::Deref for $name<'s> { |
56 type Target = &'s std::ffi::CStr; | 59 type Target = &'s CStr; |
57 fn deref(&self) -> &Self::Target { | 60 fn deref(&self) -> &Self::Target { |
58 &self.0 | 61 &self.0 |
59 } | 62 } |
60 } | 63 } |
61 | 64 |