comparison libpam-sys/src/helpers.rs @ 134:6c1e1bdb4164

Use standard #[cfg] directives rather than custom proc macros. Instead of having to do a bunch of custom parsing and other logic that tools often choke on, this change introduces an easy way to depend upon custom #[cfg]s provided by the libpam-sys crate.
author Paul Fisher <paul@pfish.zone>
date Thu, 03 Jul 2025 11:03:36 -0400
parents c77846f3a979
children
comparison
equal deleted inserted replaced
133:32b2a545ca3e 134:6c1e1bdb4164
1 //! This module contains a few non-required helpers to deal with some of the 1 //! This module contains a few non-required helpers to deal with some of the
2 //! more annoying memory management in the PAM API. 2 //! more annoying memory management in the PAM API.
3 3
4 use super::cfg_pam_impl;
5 use std::error::Error; 4 use std::error::Error;
6 use std::marker::{PhantomData, PhantomPinned}; 5 use std::marker::{PhantomData, PhantomPinned};
7 use std::mem::ManuallyDrop; 6 use std::mem::ManuallyDrop;
8 use std::ptr::NonNull; 7 use std::ptr::NonNull;
9 use std::{any, fmt, mem, slice}; 8 use std::{any, fmt, mem, slice};
131 { 130 {
132 Self::assert_size::<Src>(); 131 Self::assert_size::<Src>();
133 slice::from_raw_parts(*ptr_ptr.cast(), count).iter() 132 slice::from_raw_parts(*ptr_ptr.cast(), count).iter()
134 } 133 }
135 134
136 #[cfg_pam_impl("LinuxPam")]
137 unsafe fn _iter_over<'a, Src>(
138 ptr_ptr: *const *const Src,
139 count: usize,
140 ) -> impl Iterator<Item = &'a T>
141 where
142 T: 'a,
143 {
144 #[allow(deprecated)]
145 Self::iter_over_linux(ptr_ptr, count)
146 }
147
148 #[cfg_pam_impl(not("LinuxPam"))]
149 unsafe fn _iter_over<'a, Src>(
150 ptr_ptr: *const *const Src,
151 count: usize,
152 ) -> impl Iterator<Item = &'a T>
153 where
154 T: 'a,
155 {
156 #[allow(deprecated)]
157 Self::iter_over_xsso(ptr_ptr, count)
158 }
159
160 /// Iterates over a PAM message list appropriate to your system's impl. 135 /// Iterates over a PAM message list appropriate to your system's impl.
161 /// 136 ///
162 /// This selects the correct pointer/array structure to use for a message 137 /// This selects the correct pointer/array structure to use for a message
163 /// that was given to you by your system. 138 /// that was given to you by your system.
164 /// 139 ///
165 /// # Safety 140 /// # Safety
166 /// 141 ///
167 /// `ptr_ptr` must point to a valid message list, there must be at least 142 /// `ptr_ptr` must point to a valid message list, there must be at least
168 /// `count` messages in the list, and all messages must be a valid `Src`. 143 /// `count` messages in the list, and all messages must be a valid `Src`.
144 #[allow(deprecated)]
169 pub unsafe fn iter_over<'a, Src>( 145 pub unsafe fn iter_over<'a, Src>(
170 ptr_ptr: *const *const Src, 146 ptr_ptr: *const *const Src,
171 count: usize, 147 count: usize,
172 ) -> impl Iterator<Item = &'a T> 148 ) -> impl Iterator<Item = &'a T>
173 where 149 where
174 T: 'a, 150 T: 'a,
175 { 151 {
176 Self::_iter_over(ptr_ptr, count) 152 #[cfg(pam_impl = "LinuxPam")]
153 return Self::iter_over_linux(ptr_ptr, count);
154 #[cfg(not(pam_impl = "LinuxPam"))]
155 return Self::iter_over_xsso(ptr_ptr, count);
177 } 156 }
178 157
179 fn assert_size<That>() { 158 fn assert_size<That>() {
180 debug_assert_eq!( 159 debug_assert_eq!(
181 mem::size_of::<T>(), 160 mem::size_of::<T>(),