Skip to content

Commit 2d1299b

Browse files
authored
Merge pull request #3 from nihilistkitten/fix-clippy
Fix clippy and rustfmt errors - thanks Riley!
2 parents 14f29c8 + b2e237b commit 2d1299b

5 files changed

Lines changed: 145 additions & 42 deletions

File tree

.github/workflows/lints.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: lints
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- "*"
8+
9+
jobs:
10+
clippy:
11+
name: clippy
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: checkout source
15+
uses: actions/checkout@v2
16+
17+
- name: install nightly toolchain
18+
uses: actions-rs/toolchain@v1
19+
with:
20+
profile: minimal
21+
toolchain: nightly
22+
override: true
23+
components: rustfmt, clippy
24+
25+
- name: run cargo fmt
26+
uses: actions-rs/cargo@v1
27+
with:
28+
command: fmt
29+
args: --all -- --check
30+
31+
- name: run cargo clippy
32+
uses: actions-rs/cargo@v1
33+
with:
34+
command: clippy
35+
36+
docs:
37+
name: docs
38+
runs-on: ubuntu-latest
39+
steps:
40+
- name: checkout source
41+
uses: actions/checkout@v2
42+
43+
- name: install nightly toolchain
44+
uses: actions-rs/toolchain@v1
45+
with:
46+
profile: minimal
47+
toolchain: nightly
48+
override: true
49+
50+
- name: run cargo doc
51+
uses: actions-rs/cargo@v1
52+
with:
53+
command: doc
54+
args: --all --no-deps

src/address_space.rs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ type VirtualAddress = usize;
88
struct MapEntry {
99
source: Arc<dyn DataSource>,
1010
offset: usize,
11-
span: usize,
11+
span: usize,
1212
}
13+
14+
/// An address space.
1315
pub struct AddressSpace {
1416
name: String,
15-
mappings: LinkedList<MapEntry>, // see below for comments
17+
mappings: LinkedList<MapEntry>, // see below for comments
1618
}
1719

1820
// comments about storing mappings
@@ -26,27 +28,49 @@ pub struct AddressSpace {
2628
// from a crate (but remember it needs to be #no_std compatible), or even write your own.
2729

2830
impl AddressSpace {
31+
#[must_use]
2932
pub fn new(name: &str) -> Self {
30-
AddressSpace {
33+
Self {
3134
name: name.to_string(),
32-
mappings : LinkedList::new(),
35+
mappings: LinkedList::new(),
3336
}
3437
}
3538

36-
// add a mapping from DataSource into this AddressSpace
37-
// return VirtualAddress, or an error
38-
pub fn add_mapping(&self, source: &dyn DataSource, offset: usize, span: usize) -> Result<VirtualAddress, &str> {
39-
panic!("add mapping not yet implemented!");
39+
/// Add a mapping from a `DataSource` into this `AddressSpace`.
40+
///
41+
/// # Errors
42+
/// If the desired mapping is invalid.
43+
pub fn add_mapping(
44+
&self,
45+
source: &dyn DataSource,
46+
offset: usize,
47+
span: usize,
48+
) -> Result<VirtualAddress, &str> {
49+
todo!()
4050
}
4151

42-
// add a mapping from DataSource into this AddressSpace starting at start
43-
// returns Ok(), or an error if start + span doesn't have room for this mapping
44-
pub fn add_mapping_at(&self, source: &dyn DataSource, offset: usize, span: usize, start: VirtualAddress) -> Result<(), &str> {
45-
panic!("add mapping not yet implemented!");
52+
/// Add a mapping from `DataSource` into this `AddressSpace` starting at a specific address.
53+
///
54+
/// # Errors
55+
/// If there is insufficient room subsequent to `start`.
56+
pub fn add_mapping_at(
57+
&self,
58+
source: &dyn DataSource,
59+
offset: usize,
60+
span: usize,
61+
start: VirtualAddress,
62+
) -> Result<(), &str> {
63+
todo!()
4664
}
4765

48-
// remove the mapping to DataSource that starts at VirtualAddress
49-
pub fn remove_mapping(&self, source: &dyn DataSource, start: VirtualAddress) -> Result<(), &str> {
50-
panic!("remove_mapping not yet implemented!");
66+
/// Remove the mapping to `DataSource` that starts at the given address.
67+
/// # Errors
68+
/// If the mapping could not be removed.
69+
pub fn remove_mapping(
70+
&self,
71+
source: &dyn DataSource,
72+
start: VirtualAddress,
73+
) -> Result<(), &str> {
74+
todo!()
5175
}
52-
}
76+
}

src/cacher.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// This is the one that needs the most design.
32
// It coordinates allocating physical pages to cache data from DataSources
43
// It asks DataSources to fetch data, in response to a page fault, and when in arrives, it adds

src/data_source.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,80 @@ use std::fs::File;
33

44
pub trait DataSource {
55
// constructors are left to each implementation, once you have one, you can:
6-
fn read(&self, offset: usize, length: usize, buffer: &mut Vec<u8> ) -> Result<(), &str>;
7-
fn write(&self, offset: usize, length: usize, buffer: &mut Vec<u8> ) -> Result<(), &str>;
6+
fn read(&self, offset: usize, length: usize, buffer: &mut Vec<u8>) -> Result<(), &str>;
7+
fn write(&self, offset: usize, length: usize, buffer: &mut Vec<u8>) -> Result<(), &str>;
88
fn flush(&self, offset: usize, length: usize) -> Result<(), &str>;
9-
fn add_map(&self, with_flag: Flags, into_address_space: &mut AddressSpace, offset: usize, length: usize) -> Result<usize, &str>;
10-
fn del_map(&self, from_address_space: &mut AddressSpace, offset: usize, length: usize) -> Result<(), &str>;
9+
fn add_map(
10+
&self,
11+
with_flag: Flags,
12+
into_address_space: &mut AddressSpace,
13+
offset: usize,
14+
length: usize,
15+
) -> Result<usize, &str>;
16+
fn del_map(
17+
&self,
18+
from_address_space: &mut AddressSpace,
19+
offset: usize,
20+
length: usize,
21+
) -> Result<(), &str>;
1122
}
1223

13-
enum Flags {
24+
pub enum Flags {
1425
// TODO: do we need more flags?
15-
read,
16-
write,
17-
execute,
18-
copy_on_write,
19-
private,
20-
shared
26+
Read,
27+
Write,
28+
Execute,
29+
CopyOnWrite,
30+
Private,
31+
Shared,
2132
}
2233

34+
#[allow(clippy::module_name_repetitions)]
2335
pub struct FileDataSource {
2436
file_handle: File,
2537
name: String,
2638
}
2739

2840
impl FileDataSource {
41+
/// Create a new `FileDataSource`.
42+
///
43+
/// # Errors
44+
/// If the file can't be opened.
2945
pub fn new(name: &str) -> Result<Self, &str> {
30-
if let Ok(f) = File::open(name){
31-
Ok(FileDataSource {
32-
file_handle: f,
46+
File::open(name).map_or(Err("couldn't open {name}"), |file_handle| {
47+
Ok(Self {
48+
file_handle,
3349
name: name.to_string(),
3450
})
35-
} else {
36-
Err("couldn't open {name}")
37-
}
51+
})
3852
}
3953
}
4054

4155
impl DataSource for FileDataSource {
42-
fn read(&self, offset: usize, length: usize, buffer: &mut Vec<u8> ) -> Result<(), &str> {
56+
fn read(&self, offset: usize, length: usize, buffer: &mut Vec<u8>) -> Result<(), &str> {
4357
panic!("not yet done");
4458
}
45-
fn write(&self, offset: usize, length: usize, buffer: &mut Vec<u8> ) -> Result<(), &str>{
59+
fn write(&self, offset: usize, length: usize, buffer: &mut Vec<u8>) -> Result<(), &str> {
4660
panic!("not yet done");
4761
}
48-
fn flush(&self, offset: usize, length: usize) -> Result<(), &str>{
62+
fn flush(&self, offset: usize, length: usize) -> Result<(), &str> {
4963
panic!("not yet done");
5064
}
51-
fn add_map(&self, with_flag: Flags, into_address_space: &mut AddressSpace, offset: usize, length: usize) -> Result<usize, &str>{
65+
fn add_map(
66+
&self,
67+
with_flag: Flags,
68+
into_address_space: &mut AddressSpace,
69+
offset: usize,
70+
length: usize,
71+
) -> Result<usize, &str> {
5272
panic!("not yet done");
5373
}
54-
fn del_map(&self, from_address_space: &mut AddressSpace, offset: usize, length: usize) -> Result<(), &str>{
74+
fn del_map(
75+
&self,
76+
from_address_space: &mut AddressSpace,
77+
offset: usize,
78+
length: usize,
79+
) -> Result<(), &str> {
5580
panic!("not yet done");
5681
}
5782
}

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#![allow(dead_code, unused_variables)]
12

23
mod address_space;
3-
mod data_source;
44
mod cacher;
5+
mod data_source;
56

6-
use address_space::AddressSpace;
7-
use data_source::FileDataSource;
7+
pub use address_space::AddressSpace;
8+
pub use data_source::FileDataSource;
89

910
#[cfg(test)]
1011
mod tests {

0 commit comments

Comments
 (0)