Skip to content

Commit 68ee980

Browse files
authored
Merge pull request #9 from firefly-zero/drop-rom-funcs
Drop ROM functions
2 parents 441cb41 + e5e73bb commit 68ee980

File tree

2 files changed

+60
-127
lines changed

2 files changed

+60
-127
lines changed

examples/image/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static mut IMAGE: OnceCell<ff::FileBuf> = OnceCell::new();
1414

1515
#[no_mangle]
1616
extern fn boot() {
17-
let file = ff::rom::load_buf("img");
17+
let file = ff::load_file_buf("img").unwrap();
1818
unsafe {
1919
IMAGE.set(file).ok().unwrap();
2020
}

src/fs.rs

Lines changed: 59 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -64,133 +64,73 @@ impl<'a> File<'a> {
6464
}
6565
}
6666

67-
/// Functions for accessing files in the app ROM.
68-
pub mod rom {
69-
use super::*;
70-
71-
/// Determine the required size (in bytes) to store the given file.
72-
///
73-
/// If the file does not exist, 0 is returned.
74-
#[must_use]
75-
pub fn get_size(name: &str) -> usize {
76-
let path_ptr = name.as_ptr();
77-
let size = unsafe { bindings::get_rom_file_size(path_ptr as u32, name.len() as u32) };
78-
size as usize
79-
}
80-
81-
/// Read the whole file with the given name into the given buffer.
82-
///
83-
/// If the file size is not known in advance (and so the buffer has to be allocated
84-
/// dynamically), consider using [`load_buf`] instead.
85-
pub fn load<'a>(name: &str, buf: &'a mut [u8]) -> File<'a> {
86-
let path_ptr = name.as_ptr();
87-
let buf_ptr = buf.as_mut_ptr();
88-
unsafe {
89-
bindings::load_rom_file(
90-
path_ptr as u32,
91-
name.len() as u32,
92-
buf_ptr as u32,
93-
buf.len() as u32,
94-
);
95-
}
96-
File { raw: buf }
97-
}
67+
/// Get a file size in the data dir.
68+
///
69+
/// If the file does not exist, 0 is returned.
70+
#[must_use]
71+
pub fn get_file_size(name: &str) -> usize {
72+
let path_ptr = name.as_ptr();
73+
let size = unsafe { bindings::get_file_size(path_ptr as u32, name.len() as u32) };
74+
size as usize
75+
}
9876

99-
/// Read the whole file with the given name from ROM.
100-
///
101-
/// If you have a pre-allocated buffer of the right size, use [load] instead.
102-
///
103-
/// If the file does not exist, the returned buffer will be empty.
104-
/// This, however, should not happen in normal operation because
105-
/// the contents of the ROM directory are statically known.
106-
#[cfg(feature = "alloc")]
107-
#[must_use]
108-
pub fn load_buf(name: &str) -> FileBuf {
109-
let size = rom::get_size(name);
110-
let mut buf = vec![0; size];
111-
rom::load(name, &mut buf);
112-
FileBuf { raw: buf }
113-
}
77+
/// Read the whole file with the given name into the given buffer.
78+
///
79+
/// If the file size is not known in advance (and so the buffer has to be allocated
80+
/// dynamically), consider using [`load_buf`] instead.
81+
pub fn load_file<'a>(name: &str, buf: &'a mut [u8]) -> File<'a> {
82+
let path_ptr = name.as_ptr();
83+
let buf_ptr = buf.as_mut_ptr();
84+
unsafe {
85+
bindings::load_file(
86+
path_ptr as u32,
87+
name.len() as u32,
88+
buf_ptr as u32,
89+
buf.len() as u32,
90+
);
91+
}
92+
File { raw: buf }
11493
}
11594

116-
/// Functions for accessing files in the app data dir.
95+
/// Read the whole file with the given name.
11796
///
118-
/// Each app has an its own data dir. That directory is empty by default,
119-
/// writable by the app, and not accessible by other apps.
120-
/// Typically, it is used to store game save data.
97+
/// If you have a pre-allocated buffer of the right size, use [load] instead.
12198
///
122-
/// The device owner may empty this dir if they wish to remove the app data.
123-
pub mod data {
124-
use super::*;
125-
126-
/// Get a file size in the data dir.
127-
///
128-
/// If the file does not exist, 0 is returned.
129-
#[must_use]
130-
pub fn get_size(name: &str) -> usize {
131-
let path_ptr = name.as_ptr();
132-
let size = unsafe { bindings::get_file_size(path_ptr as u32, name.len() as u32) };
133-
size as usize
134-
}
135-
136-
/// Read the whole file with the given name into the given buffer.
137-
///
138-
/// If the file size is not known in advance (and so the buffer has to be allocated
139-
/// dynamically), consider using [`load_buf`] instead.
140-
pub fn load<'a>(name: &str, buf: &'a mut [u8]) -> File<'a> {
141-
let path_ptr = name.as_ptr();
142-
let buf_ptr = buf.as_mut_ptr();
143-
unsafe {
144-
bindings::load_file(
145-
path_ptr as u32,
146-
name.len() as u32,
147-
buf_ptr as u32,
148-
buf.len() as u32,
149-
);
150-
}
151-
File { raw: buf }
152-
}
153-
154-
/// Read the whole file with the given name from the data dir.
155-
///
156-
/// If you have a pre-allocated buffer of the right size, use [load] instead.
157-
///
158-
/// `None` is returned if the file does not exist.
159-
#[cfg(feature = "alloc")]
160-
#[must_use]
161-
pub fn load_buf(name: &str) -> Option<FileBuf> {
162-
let size = data::get_size(name);
163-
if size == 0 {
164-
return None;
165-
}
166-
let mut buf = vec![0; size];
167-
data::load(name, &mut buf);
168-
Some(FileBuf { raw: buf })
169-
}
99+
/// `None` is returned if the file does not exist.
100+
#[cfg(feature = "alloc")]
101+
#[must_use]
102+
pub fn load_file_buf(name: &str) -> Option<FileBuf> {
103+
let size = get_file_size(name);
104+
if size == 0 {
105+
return None;
106+
}
107+
let mut buf = vec![0; size];
108+
load_file(name, &mut buf);
109+
Some(FileBuf { raw: buf })
110+
}
170111

171-
/// Write the buffer into the given file in the data dir.
172-
///
173-
/// If the file exists, it will be overwritten.
174-
/// If it doesn't exist, it will be created.
175-
pub fn dump(name: &str, buf: &[u8]) {
176-
let path_ptr = name.as_ptr();
177-
let buf_ptr = buf.as_ptr();
178-
unsafe {
179-
bindings::dump_file(
180-
path_ptr as u32,
181-
name.len() as u32,
182-
buf_ptr as u32,
183-
buf.len() as u32,
184-
);
185-
}
112+
/// Write the buffer into the given file in the data dir.
113+
///
114+
/// If the file exists, it will be overwritten.
115+
/// If it doesn't exist, it will be created.
116+
pub fn dump_file(name: &str, buf: &[u8]) {
117+
let path_ptr = name.as_ptr();
118+
let buf_ptr = buf.as_ptr();
119+
unsafe {
120+
bindings::dump_file(
121+
path_ptr as u32,
122+
name.len() as u32,
123+
buf_ptr as u32,
124+
buf.len() as u32,
125+
);
186126
}
127+
}
187128

188-
/// Remove file (if exists) with the given name from the data dir.
189-
pub fn remove(name: &str) {
190-
let path_ptr = name.as_ptr();
191-
unsafe {
192-
bindings::remove_file(path_ptr as u32, name.len() as u32);
193-
}
129+
/// Remove file (if exists) with the given name from the data dir.
130+
pub fn remove_file(name: &str) {
131+
let path_ptr = name.as_ptr();
132+
unsafe {
133+
bindings::remove_file(path_ptr as u32, name.len() as u32);
194134
}
195135
}
196136

@@ -258,14 +198,7 @@ pub struct SubImage<'a> {
258198
mod bindings {
259199
#[link(wasm_import_module = "fs")]
260200
extern {
261-
pub(crate) fn get_rom_file_size(path_ptr: u32, path_len: u32) -> u32;
262201
pub(crate) fn get_file_size(path_ptr: u32, path_len: u32) -> u32;
263-
pub(crate) fn load_rom_file(
264-
path_ptr: u32,
265-
path_len: u32,
266-
buf_ptr: u32,
267-
buf_len: u32,
268-
) -> u32;
269202
pub(crate) fn load_file(path_ptr: u32, path_len: u32, buf_ptr: u32, buf_len: u32) -> u32;
270203
pub(crate) fn dump_file(path_ptr: u32, path_len: u32, buf_ptr: u32, buf_len: u32) -> u32;
271204
pub(crate) fn remove_file(path_ptr: u32, path_len: u32);

0 commit comments

Comments
 (0)