comparison src/environ.rs @ 98:b87100c5eed4

Start on environment variables, and make pointers nicer. This starts work on the PAM environment handling, and in so doing, introduces the CHeapBox and CHeapString structs. These are analogous to Box and CString, but they're located on the C heap rather than being Rust-managed memory. This is because environment variables deal with even more pointers and it turns out we can lose a lot of manual freeing using homemade smart pointers.
author Paul Fisher <paul@pfish.zone>
date Tue, 24 Jun 2025 04:25:25 -0400
parents
children 3f11b8d30f63
comparison
equal deleted inserted replaced
97:efe2f5f8b5b2 98:b87100c5eed4
1 //! Traits and stuff for managing the environment of a PAM handle.
2 //!
3 //! PAM modules can set environment variables to apply to a user session.
4 //! This module manages the interface for doing all of that.
5
6 use std::ffi::{OsStr, OsString};
7
8 /// A key/value map for environment variables, as [`OsString`]s.
9 ///
10 /// This is a limited subset of what [`HashMap`](std::collections::HashMap)
11 /// can do. Notably, we do *not* support mutable iteration.
12 pub trait EnvironMap {
13 /// Gets the environment variable of the given key.
14 fn get(&self, val: &OsStr) -> Option<&OsStr>;
15
16 /// Iterates over the key/value pairs of the environment.
17 fn iter(&self) -> impl Iterator<Item = (&OsStr, &OsStr)>;
18 }
19
20 pub trait EnvironMapMut: EnvironMap {
21 /// Sets the environment variable with the given key,
22 /// returning the old one if present.
23 fn insert(&mut self, key: &OsStr, val: &OsStr) -> Option<OsString>;
24
25 /// Removes the environment variable from the map, returning its old value
26 /// if present.
27 fn remove(&mut self, key: &OsStr) -> Option<OsString>;
28 }