Skip to content

Commit c63dad4

Browse files
tecywiz121levex
authored andcommitted
Hide most of the previously undocumented methods in a sealed trait
1 parent 7826b79 commit c63dad4

File tree

14 files changed

+106
-71
lines changed

14 files changed

+106
-71
lines changed

src/blkio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use error::*;
1010
use error::ErrorKind::*;
1111

1212
use {
13-
BlkIoResources, ControllIdentifier, Controller, Controllers, Resources, Subsystem,
13+
BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem,
1414
};
1515

1616
/// A controller that allows controlling the `blkio` subsystem of a Cgroup.
@@ -255,7 +255,7 @@ pub struct BlkIo {
255255
pub weight_device: Vec<BlkIoData>,
256256
}
257257

258-
impl Controller for BlkIoController {
258+
impl ControllerInternal for BlkIoController {
259259
fn control_type(&self) -> Controllers {
260260
Controllers::BlkIo
261261
}

src/cpu.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use error::*;
1111
use error::ErrorKind::*;
1212

1313
use {
14-
ControllIdentifier, Controller, Controllers, CpuResources, Resources, Subsystem,
14+
ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem,
1515
};
1616

1717
/// A controller that allows controlling the `cpu` subsystem of a Cgroup.
@@ -34,7 +34,7 @@ pub struct Cpu {
3434
pub stat: String,
3535
}
3636

37-
impl Controller for CpuController {
37+
impl ControllerInternal for CpuController {
3838
fn control_type(&self) -> Controllers {
3939
Controllers::Cpu
4040
}

src/cpuacct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::PathBuf;
99
use error::*;
1010
use error::ErrorKind::*;
1111

12-
use {ControllIdentifier, Controller, Controllers, Resources, Subsystem};
12+
use {ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
1313

1414
/// A controller that allows controlling the `cpuacct` subsystem of a Cgroup.
1515
///
@@ -52,7 +52,7 @@ pub struct CpuAcct {
5252
pub usage_user: u64,
5353
}
5454

55-
impl Controller for CpuAcctController {
55+
impl ControllerInternal for CpuAcctController {
5656
fn control_type(&self) -> Controllers {
5757
Controllers::CpuAcct
5858
}

src/cpuset.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use error::*;
1010
use error::ErrorKind::*;
1111

1212
use {
13-
ControllIdentifier, Controller, Controllers, CpuResources, Resources, Subsystem,
13+
ControllIdentifier, ControllerInternal, Controllers, CpuResources, Resources, Subsystem,
1414
};
1515

1616
/// A controller that allows controlling the `cpuset` subsystem of a Cgroup.
@@ -79,7 +79,7 @@ pub struct CpuSet {
7979
pub sched_relax_domain_level: u64,
8080
}
8181

82-
impl Controller for CpuSetController {
82+
impl ControllerInternal for CpuSetController {
8383
fn control_type(&self) -> Controllers {
8484
Controllers::CpuSet
8585
}

src/devices.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use error::*;
99
use error::ErrorKind::*;
1010

1111
use {
12-
ControllIdentifier, Controller, Controllers, DeviceResource, DeviceResources,
12+
ControllIdentifier, ControllerInternal, Controllers, DeviceResource, DeviceResources,
1313
Resources, Subsystem,
1414
};
1515

@@ -131,7 +131,7 @@ impl DevicePermissions {
131131
}
132132
}
133133

134-
impl Controller for DevicesController {
134+
impl ControllerInternal for DevicesController {
135135
fn control_type(&self) -> Controllers {
136136
Controllers::Devices
137137
}

src/freezer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::PathBuf;
88
use error::*;
99
use error::ErrorKind::*;
1010

11-
use {ControllIdentifier, Controller, Controllers, Resources, Subsystem};
11+
use {ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
1212

1313
/// A controller that allows controlling the `freezer` subsystem of a Cgroup.
1414
///
@@ -34,7 +34,7 @@ pub enum FreezerState {
3434
Frozen,
3535
}
3636

37-
impl Controller for FreezerController {
37+
impl ControllerInternal for FreezerController {
3838
fn control_type(&self) -> Controllers {
3939
Controllers::Freezer
4040
}

src/hugetlb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use error::*;
1010
use error::ErrorKind::*;
1111

1212
use {
13-
ControllIdentifier, Controller, Controllers, HugePageResources, Resources,
13+
ControllIdentifier, ControllerInternal, Controllers, HugePageResources, Resources,
1414
Subsystem,
1515
};
1616

@@ -24,7 +24,7 @@ pub struct HugeTlbController {
2424
path: PathBuf,
2525
}
2626

27-
impl Controller for HugeTlbController {
27+
impl ControllerInternal for HugeTlbController {
2828
fn control_type(&self) -> Controllers {
2929
Controllers::HugeTlb
3030
}

src/lib.rs

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -108,31 +108,95 @@ impl Controllers {
108108
}
109109
}
110110

111+
mod sealed {
112+
use super::*;
113+
114+
pub trait ControllerInternal {
115+
fn apply(&self, res: &Resources) -> Result<()>;
116+
117+
// meta stuff
118+
fn control_type(&self) -> Controllers;
119+
fn get_path(&self) -> &PathBuf;
120+
fn get_path_mut(&mut self) -> &mut PathBuf;
121+
fn get_base(&self) -> &PathBuf;
122+
123+
fn verify_path(&self) -> Result<()> {
124+
if self.get_path().starts_with(self.get_base()) {
125+
Ok(())
126+
} else {
127+
Err(Error::new(ErrorKind::InvalidPath))
128+
}
129+
}
130+
131+
fn open_path(&self, p: &str, w: bool) -> Result<File> {
132+
let mut path = self.get_path().clone();
133+
path.push(p);
134+
135+
self.verify_path()?;
136+
137+
if w {
138+
match File::create(&path) {
139+
Err(e) => return Err(Error::with_cause(ErrorKind::WriteFailed, e)),
140+
Ok(file) => return Ok(file),
141+
}
142+
} else {
143+
match File::open(&path) {
144+
Err(e) => return Err(Error::with_cause(ErrorKind::ReadFailed, e)),
145+
Ok(file) => return Ok(file),
146+
}
147+
}
148+
}
149+
150+
#[doc(hidden)]
151+
fn path_exists(&self, p: &str) -> bool {
152+
if let Err(_) = self.verify_path() {
153+
return false;
154+
}
155+
156+
std::path::Path::new(p).exists()
157+
}
158+
159+
}
160+
}
161+
162+
pub(crate) use sealed::ControllerInternal;
163+
111164
/// A Controller is a subsystem attached to the control group.
112165
///
113166
/// Implementors are able to control certain aspects of a control group.
114167
pub trait Controller {
168+
#[doc(hidden)]
169+
fn control_type(&self) -> Controllers;
170+
115171
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
116172
/// kernel the information.
117173
fn apply(&self, res: &Resources) -> Result<()>;
118174

119-
// meta stuff
120-
#[doc(hidden)]
121-
fn control_type(&self) -> Controllers;
122-
#[doc(hidden)]
123-
fn get_path(&self) -> &PathBuf;
124-
#[doc(hidden)]
125-
fn get_path_mut(&mut self) -> &mut PathBuf;
126-
#[doc(hidden)]
127-
fn get_base(&self) -> &PathBuf;
175+
/// Create this controller
176+
fn create(&self);
128177

129-
#[doc(hidden)]
130-
fn verify_path(&self) -> Result<()> {
131-
if self.get_path().starts_with(self.get_base()) {
132-
Ok(())
133-
} else {
134-
Err(Error::new(ErrorKind::InvalidPath))
135-
}
178+
/// Does this controller already exist?
179+
fn exists(&self) -> bool;
180+
181+
/// Delete the controller.
182+
fn delete(&self);
183+
184+
/// Attach a task to this controller.
185+
fn add_task(&self, pid: &CgroupPid) -> Result<()>;
186+
187+
/// Get the list of tasks that this controller has.
188+
fn tasks(&self) -> Vec<CgroupPid>;
189+
}
190+
191+
impl<T> Controller for T where T: ControllerInternal {
192+
fn control_type(&self) -> Controllers {
193+
ControllerInternal::control_type(self)
194+
}
195+
196+
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
197+
/// kernel the information.
198+
fn apply(&self, res: &Resources) -> Result<()> {
199+
ControllerInternal::apply(self, res)
136200
}
137201

138202
/// Create this controller
@@ -157,35 +221,6 @@ pub trait Controller {
157221
}
158222
}
159223

160-
#[doc(hidden)]
161-
fn open_path(&self, p: &str, w: bool) -> Result<File> {
162-
let mut path = self.get_path().clone();
163-
path.push(p);
164-
165-
self.verify_path()?;
166-
167-
if w {
168-
match File::create(&path) {
169-
Err(e) => return Err(Error::with_cause(ErrorKind::WriteFailed, e)),
170-
Ok(file) => return Ok(file),
171-
}
172-
} else {
173-
match File::open(&path) {
174-
Err(e) => return Err(Error::with_cause(ErrorKind::ReadFailed, e)),
175-
Ok(file) => return Ok(file),
176-
}
177-
}
178-
}
179-
180-
#[doc(hidden)]
181-
fn path_exists(&self, p: &str) -> bool {
182-
if let Err(_) = self.verify_path() {
183-
return false;
184-
}
185-
186-
std::path::Path::new(p).exists()
187-
}
188-
189224
/// Attach a task to this controller.
190225
fn add_task(&self, pid: &CgroupPid) -> Result<()> {
191226
self.open_path("tasks", true).and_then(|mut file| {

src/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use error::*;
1010
use error::ErrorKind::*;
1111

1212
use {
13-
ControllIdentifier, Controller, Controllers, MemoryResources, Resources, Subsystem,
13+
ControllIdentifier, ControllerInternal, Controllers, MemoryResources, Resources, Subsystem,
1414
};
1515

1616
/// A controller that allows controlling the `memory` subsystem of a Cgroup.
@@ -393,7 +393,7 @@ pub struct Kmem {
393393
pub slabinfo: String,
394394
}
395395

396-
impl Controller for MemController {
396+
impl ControllerInternal for MemController {
397397
fn control_type(&self) -> Controllers {
398398
Controllers::Mem
399399
}

src/net_cls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use error::*;
1010
use error::ErrorKind::*;
1111

1212
use {
13-
ControllIdentifier, Controller, Controllers, NetworkResources, Resources,
13+
ControllIdentifier, ControllerInternal, Controllers, NetworkResources, Resources,
1414
Subsystem,
1515
};
1616

@@ -25,7 +25,7 @@ pub struct NetClsController {
2525
path: PathBuf,
2626
}
2727

28-
impl Controller for NetClsController {
28+
impl ControllerInternal for NetClsController {
2929
fn control_type(&self) -> Controllers {
3030
Controllers::NetCls
3131
}

0 commit comments

Comments
 (0)