Skip to content

Commit 98d72ae

Browse files
committed
cgrouprs: Enhance CgroupPid
Implement `From<u32>` and `From<i32>` for `CgroupPid` to allow conversion from these types directly. Add `as_raw()` method to retrieve the raw PID value, and add `set()` method to modify the PID value. Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
1 parent 42d8a71 commit 98d72ae

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum FreezerState {
3434
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
3535
pub struct CgroupPid {
3636
/// The process identifier
37-
pub pid: u64,
37+
pid: u64,
3838
}
3939

4040
impl From<u64> for CgroupPid {
@@ -49,6 +49,28 @@ impl From<&std::process::Child> for CgroupPid {
4949
}
5050
}
5151

52+
impl From<u32> for CgroupPid {
53+
fn from(u: u32) -> CgroupPid {
54+
CgroupPid { pid: u as u64 }
55+
}
56+
}
57+
58+
impl From<i32> for CgroupPid {
59+
fn from(u: i32) -> CgroupPid {
60+
CgroupPid { pid: u as u64 }
61+
}
62+
}
63+
64+
impl CgroupPid {
65+
pub fn set(&mut self, pid: u64) {
66+
self.pid = pid;
67+
}
68+
69+
pub fn as_raw(&self) -> u64 {
70+
self.pid
71+
}
72+
}
73+
5274
#[cfg(test)]
5375
pub mod tests {
5476
use std::fs;

0 commit comments

Comments
 (0)