@@ -3,55 +3,80 @@ use std::fs::File;
33
44pub 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) ]
2335pub struct FileDataSource {
2436 file_handle : File ,
2537 name : String ,
2638}
2739
2840impl 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
4155impl 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}
0 commit comments