mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 08:11:12 +00:00
Drop roc_fx_readAll
It's really not doing anything significant that roc_fx_open and roc_fx_read aren't already.
This commit is contained in:
parent
804c00c899
commit
a12b421e9a
1 changed files with 0 additions and 78 deletions
|
@ -163,84 +163,6 @@ pub fn rust_main() -> isize {
|
|||
/// A C file descriptor.
|
||||
pub struct Fd(c_int);
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn roc_fx_readAll(roc_path: RocStr) -> RocResult<RocList<u8>, Errno> {
|
||||
const BUF_BYTES: usize = 1024;
|
||||
|
||||
// I know that since Rust 1.39 mem::uninitialized() is deprecated in favor
|
||||
// of MaybeUninit, but I couldn't get this to work with MaybeUninit.
|
||||
#[allow(deprecated)]
|
||||
let mut buf: [u8; BUF_BYTES] = mem::uninitialized();
|
||||
let fd: c_int;
|
||||
|
||||
// If the path fits in the stack-allocated buffer we'll later use for
|
||||
// buffered reading, then we can use it now to avoid a heap allocation
|
||||
// when translating our `RocStr` to a null-terminated `char*`.
|
||||
{
|
||||
let path_len = roc_path.len();
|
||||
let path_fits_in_buf = path_len > BUF_BYTES;
|
||||
let c_path: *mut c_char;
|
||||
|
||||
if path_fits_in_buf {
|
||||
roc_path.write_c_str(&mut buf as *mut u8);
|
||||
|
||||
c_path = buf.as_mut_ptr() as *mut c_char;
|
||||
} else {
|
||||
c_path = roc_alloc(path_len, mem::align_of::<c_char>() as u32) as *mut c_char;
|
||||
|
||||
roc_path.write_c_str(c_path as *mut u8);
|
||||
}
|
||||
|
||||
fd = libc::open(c_path, libc::O_RDONLY);
|
||||
|
||||
// Deallocate c_path now (if necessary) so we can safely do early returns
|
||||
// from here on.
|
||||
if !path_fits_in_buf {
|
||||
roc_dealloc(c_path as *mut c_void, mem::align_of_val(&c_path) as u32);
|
||||
}
|
||||
}
|
||||
|
||||
// We'll use our own position and libc::pread rather than using libc::read
|
||||
// repeatedly and letting the fd store its own position. This way we don't
|
||||
// have to worry about concurrent modifications of the fd's position.
|
||||
let mut position = 0;
|
||||
|
||||
// if libc::open returned -1, that means there was an error
|
||||
if fd != -1 {
|
||||
let mut list = RocList::empty();
|
||||
|
||||
loop {
|
||||
let bytes_read = libc::pread(fd, buf.as_mut_ptr() as *mut c_void, BUF_BYTES, position);
|
||||
|
||||
if bytes_read == BUF_BYTES as isize {
|
||||
// The read was successful, and there may be more bytes to read.
|
||||
// Append the bytes to the list and continue looping!
|
||||
let slice = core::slice::from_raw_parts(buf.as_ptr(), bytes_read as usize);
|
||||
|
||||
list.append_slice(slice);
|
||||
} else if bytes_read >= 0 {
|
||||
// The read was successful, and there are no more bytes
|
||||
// to read (because bytes_read was less than the requested
|
||||
// BUF_BYTES, but it was also not negative - which would have
|
||||
// indicated an error).
|
||||
let slice = core::slice::from_raw_parts(buf.as_ptr(), bytes_read as usize);
|
||||
|
||||
list.append_slice(slice);
|
||||
|
||||
// We're done!
|
||||
return RocResult::Ok(list);
|
||||
} else {
|
||||
// bytes_read was negative, so we got a read error!
|
||||
break;
|
||||
}
|
||||
|
||||
position += bytes_read as i64;
|
||||
}
|
||||
}
|
||||
|
||||
RocResult::Err(errno())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe fn roc_fx_open(roc_path: RocStr) -> RocResult<Fd, Errno> {
|
||||
const BUF_BYTES: usize = 1024;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue