Mercurial > crates > nonstick
diff src/libpam/memory.rs @ 79:2128123b9406
Format (oops!) and make some fun and/or stupid conversions available.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 08 Jun 2025 04:21:58 -0400 |
parents | 002adfb98c5c |
children | 5aa1a010f1e8 |
line wrap: on
line diff
--- a/src/libpam/memory.rs Sun Jun 08 03:48:40 2025 -0400 +++ b/src/libpam/memory.rs Sun Jun 08 04:21:58 2025 -0400 @@ -87,6 +87,7 @@ if data.contains(&0) { return Err(ErrorCode::ConversationError); } + // +1 for the null terminator let data_alloc: *mut c_char = calloc(data.len() + 1); // SAFETY: we just allocated this and we have enough room. unsafe { @@ -158,29 +159,37 @@ } } -impl<'a> From<&'a CBinaryData> for (&'a[u8], u8) { +impl<'a> From<&'a CBinaryData> for (&'a [u8], u8) { fn from(value: &'a CBinaryData) -> Self { - (unsafe { slice::from_raw_parts(value.data.as_ptr(), value.length()) }, - value.data_type ) + ( + unsafe { slice::from_raw_parts(value.data.as_ptr(), value.length()) }, + value.data_type, + ) + } +} + +impl From<&'_ CBinaryData> for BinaryData { + fn from(value: &'_ CBinaryData) -> Self { + // This is a dumb trick but I like it because it is simply the presence + // of `.map(|z: (_, _)| z)` in the middle of this that gives + // type inference the hint it needs to make this work. + let [ret] = [value].map(Into::into).map(|z: (_, _)| z).map(Into::into); + ret } } impl From<Option<&'_ CBinaryData>> for BinaryData { fn from(value: Option<&CBinaryData>) -> Self { - // This is a dumb trick but I like it because it is simply the presence - // of `.map(|(x, y)| (x, y))` in the middle of this that gives - // type inference the hint it needs to make this work. - value - .map(Into::into) - .map(|(data, data_type)| (data, data_type)) - .map(Into::into) - .unwrap_or_default() + value.map(Into::into).unwrap_or_default() } } #[cfg(test)] mod tests { - use super::{free, ErrorCode, CString, copy_pam_string, malloc_str, option_cstr, prompt_ptr, zero_c_string}; + use super::{ + copy_pam_string, free, malloc_str, option_cstr, prompt_ptr, zero_c_string, CString, + ErrorCode, + }; #[test] fn test_strings() { let str = malloc_str("hello there").unwrap();