Skip to content

Commit c0ff362

Browse files
committed
Fix all warnings from the compiler
This change simply fixes all the warnings from the latest version of the compiler. It also removes the (non-test code) unsafe blocks. One of the warnings I think was actually a bug - a cgroup builder could fail to apply resource constraints but still return a Cgroup silently. Now a builder will return an Error correctly in this case - this is an API change though so requires a mv bump.
1 parent ed1e816 commit c0ff362

25 files changed

+373
-452
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repository = "https://github.com/levex/cgroups-rs"
55
keywords = ["linux", "cgroup", "containers", "isolation"]
66
categories = ["os", "api-bindings", "os::unix-apis"]
77
license = "MIT OR Apache-2.0"
8-
version = "0.1.1-alpha.0"
8+
version = "0.2.1-alpha.0"
99
authors = ["Levente Kurusa <[email protected]>", "Sam Wilson <[email protected]>"]
1010
edition = "2018"
1111

src/blkio.rs

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::fs::File;
66
use std::io::{Read, Write};
77
use std::path::PathBuf;
88

9-
use crate::error::*;
109
use crate::error::ErrorKind::*;
10+
use crate::error::*;
1111

1212
use crate::{
1313
BlkIoResources, ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem,
@@ -279,7 +279,8 @@ impl ControllerInternal for BlkIoController {
279279

280280
for dev in &res.weight_device {
281281
let _ = self.set_weight_for_device(dev.major, dev.minor, dev.weight as u64);
282-
let _ = self.set_leaf_weight_for_device(dev.major, dev.minor, dev.leaf_weight as u64);
282+
let _ =
283+
self.set_leaf_weight_for_device(dev.major, dev.minor, dev.leaf_weight as u64);
283284
}
284285

285286
for dev in &res.throttle_read_bps_device {
@@ -309,19 +310,7 @@ impl ControllIdentifier for BlkIoController {
309310
}
310311
}
311312

312-
impl<'a> From<&'a Subsystem> for &'a BlkIoController {
313-
fn from(sub: &'a Subsystem) -> &'a BlkIoController {
314-
unsafe {
315-
match sub {
316-
Subsystem::BlkIo(c) => c,
317-
_ => {
318-
assert_eq!(1, 0);
319-
::std::mem::uninitialized()
320-
}
321-
}
322-
}
323-
}
324-
}
313+
impl_from_subsystem_for_controller!(Subsystem::BlkIo, BlkIoController);
325314

326315
fn read_string_from(mut file: File) -> Result<String> {
327316
let mut string = String::new();
@@ -334,7 +323,10 @@ fn read_string_from(mut file: File) -> Result<String> {
334323
fn read_u64_from(mut file: File) -> Result<u64> {
335324
let mut string = String::new();
336325
match file.read_to_string(&mut string) {
337-
Ok(_) => string.trim().parse().map_err(|e| Error::with_cause(ParseError, e)),
326+
Ok(_) => string
327+
.trim()
328+
.parse()
329+
.map_err(|e| Error::with_cause(ParseError, e)),
338330
Err(e) => Err(Error::with_cause(ReadFailed, e)),
339331
}
340332
}
@@ -588,12 +580,7 @@ impl BlkIoController {
588580
}
589581

590582
/// Same as `set_leaf_weight()`, but settable per each block device.
591-
pub fn set_leaf_weight_for_device(
592-
&self,
593-
major: u64,
594-
minor: u64,
595-
weight: u64,
596-
) -> Result<()> {
583+
pub fn set_leaf_weight_for_device(&self, major: u64, minor: u64, weight: u64) -> Result<()> {
597584
self.open_path("blkio.leaf_weight_device", true)
598585
.and_then(|mut file| {
599586
file.write_all(format!("{}:{} {}", major, minor, weight).as_ref())
@@ -612,12 +599,7 @@ impl BlkIoController {
612599

613600
/// Throttle the bytes per second rate of read operation affecting the block device
614601
/// `major:minor` to `bps`.
615-
pub fn throttle_read_bps_for_device(
616-
&self,
617-
major: u64,
618-
minor: u64,
619-
bps: u64,
620-
) -> Result<()> {
602+
pub fn throttle_read_bps_for_device(&self, major: u64, minor: u64, bps: u64) -> Result<()> {
621603
self.open_path("blkio.throttle.read_bps_device", true)
622604
.and_then(|mut file| {
623605
file.write_all(format!("{}:{} {}", major, minor, bps).to_string().as_ref())
@@ -627,12 +609,7 @@ impl BlkIoController {
627609

628610
/// Throttle the I/O operations per second rate of read operation affecting the block device
629611
/// `major:minor` to `bps`.
630-
pub fn throttle_read_iops_for_device(
631-
&self,
632-
major: u64,
633-
minor: u64,
634-
iops: u64,
635-
) -> Result<()> {
612+
pub fn throttle_read_iops_for_device(&self, major: u64, minor: u64, iops: u64) -> Result<()> {
636613
self.open_path("blkio.throttle.read_iops_device", true)
637614
.and_then(|mut file| {
638615
file.write_all(format!("{}:{} {}", major, minor, iops).to_string().as_ref())
@@ -641,12 +618,7 @@ impl BlkIoController {
641618
}
642619
/// Throttle the bytes per second rate of write operation affecting the block device
643620
/// `major:minor` to `bps`.
644-
pub fn throttle_write_bps_for_device(
645-
&self,
646-
major: u64,
647-
minor: u64,
648-
bps: u64,
649-
) -> Result<()> {
621+
pub fn throttle_write_bps_for_device(&self, major: u64, minor: u64, bps: u64) -> Result<()> {
650622
self.open_path("blkio.throttle.write_bps_device", true)
651623
.and_then(|mut file| {
652624
file.write_all(format!("{}:{} {}", major, minor, bps).to_string().as_ref())
@@ -656,12 +628,7 @@ impl BlkIoController {
656628

657629
/// Throttle the I/O operations per second rate of write operation affecting the block device
658630
/// `major:minor` to `bps`.
659-
pub fn throttle_write_iops_for_device(
660-
&self,
661-
major: u64,
662-
minor: u64,
663-
iops: u64,
664-
) -> Result<()> {
631+
pub fn throttle_write_iops_for_device(&self, major: u64, minor: u64, iops: u64) -> Result<()> {
665632
self.open_path("blkio.throttle.write_iops_device", true)
666633
.and_then(|mut file| {
667634
file.write_all(format!("{}:{} {}", major, minor, iops).to_string().as_ref())
@@ -671,20 +638,14 @@ impl BlkIoController {
671638

672639
/// Set the weight of the control group's tasks.
673640
pub fn set_weight(&self, w: u64) -> Result<()> {
674-
self.open_path("blkio.weight", true)
675-
.and_then(|mut file| {
676-
file.write_all(w.to_string().as_ref())
677-
.map_err(|e| Error::with_cause(WriteFailed, e))
678-
})
641+
self.open_path("blkio.weight", true).and_then(|mut file| {
642+
file.write_all(w.to_string().as_ref())
643+
.map_err(|e| Error::with_cause(WriteFailed, e))
644+
})
679645
}
680646

681647
/// Same as `set_weight()`, but settable per each block device.
682-
pub fn set_weight_for_device(
683-
&self,
684-
major: u64,
685-
minor: u64,
686-
weight: u64,
687-
) -> Result<()> {
648+
pub fn set_weight_for_device(&self, major: u64, minor: u64, weight: u64) -> Result<()> {
688649
self.open_path("blkio.weight_device", true)
689650
.and_then(|mut file| {
690651
file.write_all(format!("{}:{} {}", major, minor, weight).as_ref())
@@ -755,10 +716,7 @@ Total 61823067136
755716
#[test]
756717
fn test_parse_io_service_total() {
757718
let ok = parse_io_service_total(TEST_VALUE.to_string()).unwrap();
758-
assert_eq!(
759-
ok,
760-
61823067136
761-
);
719+
assert_eq!(ok, 61823067136);
762720
}
763721

764722
#[test]
@@ -806,10 +764,7 @@ Total 61823067136
806764
]
807765
);
808766
let err = parse_io_service(TEST_WRONG_VALUE.to_string()).unwrap_err();
809-
assert_eq!(
810-
err.kind(),
811-
&ErrorKind::ParseError,
812-
);
767+
assert_eq!(err.kind(), &ErrorKind::ParseError,);
813768
}
814769

815770
#[test]

src/cgroup.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct Cgroup<'b> {
2424
subsystems: Vec<Subsystem>,
2525

2626
/// The hierarchy.
27-
hier: &'b Hierarchy,
27+
hier: &'b dyn Hierarchy,
2828
}
2929

3030
impl<'b> Cgroup<'b> {
@@ -41,7 +41,7 @@ impl<'b> Cgroup<'b> {
4141
///
4242
/// Note that if the handle goes out of scope and is dropped, the control group is _not_
4343
/// destroyed.
44-
pub fn new<P: AsRef<Path>>(hier: &Hierarchy, path: P) -> Cgroup {
44+
pub fn new<P: AsRef<Path>>(hier: &dyn Hierarchy, path: P) -> Cgroup {
4545
let cg = Cgroup::load(hier, path);
4646
cg.create();
4747
cg
@@ -54,7 +54,7 @@ impl<'b> Cgroup<'b> {
5454
///
5555
/// Note that if the handle goes out of scope and is dropped, the control group is _not_
5656
/// destroyed.
57-
pub fn load<P: AsRef<Path>>(hier: &Hierarchy, path: P) -> Cgroup {
57+
pub fn load<P: AsRef<Path>>(hier: &dyn Hierarchy, path: P) -> Cgroup {
5858
let path = path.as_ref();
5959
let mut subsystems = hier.subsystems();
6060
if path.as_os_str() != "" {

0 commit comments

Comments
 (0)