comparison src/libpam/memory.rs @ 180:a1bb1d013567

Remove `syn` from the dependency tree by implementing our own num_enum.
author Paul Fisher <paul@pfish.zone>
date Wed, 30 Jul 2025 18:22:16 -0400
parents 634cd5f2ac8b
children fb8b547b36b7
comparison
equal deleted inserted replaced
179:dba9c1f40883 180:a1bb1d013567
6 use std::mem::ManuallyDrop; 6 use std::mem::ManuallyDrop;
7 use std::ops::{Deref, DerefMut}; 7 use std::ops::{Deref, DerefMut};
8 use std::os::unix::ffi::{OsStrExt, OsStringExt}; 8 use std::os::unix::ffi::{OsStrExt, OsStringExt};
9 use std::ptr::NonNull; 9 use std::ptr::NonNull;
10 use std::{mem, ptr, slice}; 10 use std::{mem, ptr, slice};
11
12 /// Like the num_enum crate, but with no dependency on `syn`.
13 macro_rules! num_enum {
14 (
15 $(#[$m:meta])*
16 $viz:vis enum $name:ident($repr:ty) {
17 $(
18 $(#[$im:meta])*
19 $item_name:ident = $item_value:path,
20 )*
21 }
22 ) => {
23 $(#[$m])*
24 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
25 #[repr($repr)]
26 $viz enum $name {
27 $(
28 $(#[$im])*
29 $item_name = $item_value,
30 )*
31 }
32
33 impl TryFrom<$repr> for $name {
34 type Error = crate::constants::ErrorCode;
35
36 #[allow(unused_doc_comments)]
37 fn try_from(value: $repr) -> crate::constants::Result<$name> {
38 match value {
39 $(
40 $(#[$im])*
41 $item_value => Ok(Self::$item_name),
42 )*
43 _ => Err(crate::constants::ErrorCode::BAD_CONST),
44 }
45 }
46 }
47
48 impl From<$name> for $repr {
49 fn from(value: $name) -> $repr {
50 value as $repr
51 }
52 }
53 }
54 }
55
56 pub(crate) use num_enum;
11 57
12 /// Allocates `count` elements to hold `T`. 58 /// Allocates `count` elements to hold `T`.
13 #[inline] 59 #[inline]
14 pub fn calloc<T>(count: usize) -> NonNull<T> { 60 pub fn calloc<T>(count: usize) -> NonNull<T> {
15 // SAFETY: it's always safe to allocate! Leaking memory is fun! 61 // SAFETY: it's always safe to allocate! Leaking memory is fun!