Skip to content

Commit 41c5612

Browse files
committed
format, tidy
1 parent 5a5b424 commit 41c5612

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

library/std/src/sys/fs/vexos.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ impl OpenOptions {
194194
}
195195

196196
impl File {
197-
// TODO
198197
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
199198
// Mount sdcard volume as FAT filesystem
200199
map_fresult(unsafe { vex_sdk::vexFileMountSD() })?;

library/std/src/sys/pal/vexos/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ pub mod pipe;
55
pub mod thread;
66
pub mod time;
77

8-
use crate::sys::stdio;
98
use crate::arch::global_asm;
109
use crate::ptr::{self, addr_of_mut};
10+
use crate::sys::stdio;
1111
use crate::time::{Duration, Instant};
1212

1313
global_asm!(
@@ -60,8 +60,7 @@ pub unsafe fn cleanup() {
6060
vex_sdk::vexTasksRun();
6161

6262
// If the buffer has been fully flushed, exit the loop
63-
if vex_sdk::vexSerialWriteFree(stdio::STDIO_CHANNEL) == (stdio::STDOUT_BUF_SIZE as i32)
64-
{
63+
if vex_sdk::vexSerialWriteFree(stdio::STDIO_CHANNEL) == (stdio::STDOUT_BUF_SIZE as i32) {
6564
break;
6665
}
6766
}

library/std/src/sys/stdio/vexos.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Stdin {
1414

1515
impl io::Read for Stdin {
1616
fn read(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
17-
let mut written = 0;
17+
let mut count = 0;
1818

1919
while let Some((out_byte, new_buf)) = buf.split_first_mut() {
2020
buf = new_buf;
@@ -25,10 +25,10 @@ impl io::Read for Stdin {
2525
}
2626

2727
*out_byte = byte as u8;
28-
written += 1;
28+
count += 1;
2929
}
3030

31-
Ok(written)
31+
Ok(count)
3232
}
3333
}
3434

@@ -40,32 +40,48 @@ impl Stdout {
4040

4141
impl io::Write for Stdout {
4242
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
43-
let mut count = 0;
43+
let mut written = 0;
4444

45-
// HACK: VEXos holds an internal write buffer for serial that is flushed to USB1 roughly every
46-
// millisecond by `vexTasksRun`. For writes larger than 2048 bytes, we must block until that buffer
47-
// is flushed to USB1 before writing the rest of `buf`. In practice, this is fairly nonstandard for
48-
// a `write` implementation but it avoids an guaranteed recursive panic when using macros such as
49-
// `print!` to write large amounts of data to stdout at once.
45+
// HACK: VEXos holds an internal ringbuffer for serial writes that is flushed to USB1
46+
// roughly every millisecond by `vexTasksRun`. For writes larger than 2048 bytes, we
47+
// must block until that buffer is flushed to USB1 before writing the rest of `buf`.
48+
//
49+
// This is fairly nonstandard for a `write` implementation, but it avoids a guaranteed
50+
// recursive panic when using macros such as `print!` to write large amounts of data
51+
// (buf.len() > 2048) to stdout at once.
5052
for chunk in buf.chunks(STDOUT_BUF_SIZE) {
5153
if unsafe { vex_sdk::vexSerialWriteFree(STDIO_CHANNEL) as usize } < chunk.len() {
5254
self.flush().unwrap();
5355
}
5456

55-
count += unsafe { vex_sdk::vexSerialWriteBuffer(STDIO_CHANNEL, chunk.as_ptr(), chunk.len() as u32) };
56-
57+
let count = unsafe {
58+
vex_sdk::vexSerialWriteBuffer(STDIO_CHANNEL, chunk.as_ptr(), chunk.len() as u32)
59+
};
5760
if count < 0 {
5861
return Err(io::Error::new(
5962
io::ErrorKind::Uncategorized,
6063
"Internal write error occurred.",
6164
));
6265
}
66+
67+
written += count;
68+
69+
// This is a sanity check to ensure that we don't end up with non-contiguous
70+
// buffer writes. e.g. a chunk gets only partially written, but we continue
71+
// attempting to write the remaining chunks.
72+
//
73+
// In practice, this should never really occur since the previous flush ensures
74+
// enough space in FIFO to write the entire chunk to vexSerialWriteBuffer.
75+
if count != chunk.len() {
76+
break;
77+
}
6378
}
6479

65-
Ok(count as usize)
80+
Ok(written as usize)
6681
}
6782

6883
fn flush(&mut self) -> io::Result<()> {
84+
// This may block for up to a millisecond.
6985
unsafe {
7086
while (vex_sdk::vexSerialWriteFree(STDIO_CHANNEL) as usize) != STDOUT_BUF_SIZE {
7187
vex_sdk::vexTasksRun();

0 commit comments

Comments
 (0)