1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::ffi::{OsStr, OsString};
use std::fmt;

use crate::sys;
use heim_common::prelude::wrap;

/// Process environment variables.
///
/// This structure is created by [`Process::environment`] method.
/// See its documentation for more.
///
/// [`Process::environment`]: ./struct.Process.html#method.environment
pub struct Environment(sys::Environment);

wrap!(Environment, sys::Environment);

impl Environment {
    /// Returns a non-consuming iterator over environment variables.
    pub fn iter(&self) -> EnvironmentIter<'_> {
        EnvironmentIter::from(self.0.iter())
    }
}

impl fmt::Debug for Environment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

impl IntoIterator for Environment {
    type Item = (OsString, OsString);
    type IntoIter = IntoEnvironmentIter;

    fn into_iter(self) -> Self::IntoIter {
        IntoEnvironmentIter::from(self.0.into_iter())
    }
}

impl<'e> IntoIterator for &'e Environment {
    type Item = (&'e OsStr, &'e OsStr);
    type IntoIter = EnvironmentIter<'e>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator that moves out from the process environment variables.
///
/// This struct is created by the `into_iter` method on [`Environment`].
///
/// [`Environment`]: ./struct.Environment.html
#[derive(Debug)]
pub struct IntoEnvironmentIter(sys::IntoEnvironmentIter);

wrap!(IntoEnvironmentIter, sys::IntoEnvironmentIter);

impl Iterator for IntoEnvironmentIter {
    type Item = (OsString, OsString);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

/// A non-consuming iterator for the process environment variables.
///
/// This struct is created by the `iter` method on [`Environment`].
///
/// [`Environment`]: ./struct.Environment.html
#[derive(Debug)]
pub struct EnvironmentIter<'e>(sys::EnvironmentIter<'e>);

wrap!('e, EnvironmentIter<'e>, sys::EnvironmentIter<'e>);

impl<'e> Iterator for EnvironmentIter<'e> {
    type Item = (&'e OsStr, &'e OsStr);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}