Skip to content

Commit b34b6f4

Browse files
Added NSTDFileResult.
1 parent 5a3e272 commit b34b6f4

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
### `nstd.cstring`
1515
- Added `from_cstr`.
1616
- Renamed `to_bytes` to `into_bytes`.
17+
### `nstd.fs`
18+
- Added `NSTDFileResult`.
1719
### `nstd.os`
1820
- Added `[unix|windows].shared_lib`.
1921
- Added `NSTDWindowsHeapResult`.

include/nstd/fs/file.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef NSTD_FS_FILE_H
22
#define NSTD_FS_FILE_H
3+
#include "../core/result.h"
34
#include "../core/slice.h"
45
#include "../core/str.h"
56
#include "../io/io.h"
@@ -28,6 +29,9 @@
2829
/// A handle to an opened file.
2930
typedef NSTDAnyMut NSTDFile;
3031

32+
/// A result type yielding an `NSTDFile` on success.
33+
NSTDResult(NSTDFile, NSTDIOError) NSTDFileResult;
34+
3135
/// Opens file on the filesystem and returns a handle to it.
3236
///
3337
/// # Parameters:
@@ -36,11 +40,9 @@ typedef NSTDAnyMut NSTDFile;
3640
///
3741
/// - `NSTDUInt8 mask` - A bit mask for toggling the file's different open options.
3842
///
39-
/// - `NSTDIOError *errc` - Returns as the I/O operation error code.
40-
///
4143
/// # Returns
4244
///
43-
/// `NSTDFile file` - A handle to the opened file, or null if an error occurs.
45+
/// `NSTDFileResult file` - A handle to the opened file, or the IO error on failure.
4446
///
4547
/// # Panics
4648
///
@@ -49,7 +51,7 @@ typedef NSTDAnyMut NSTDFile;
4951
/// # Safety
5052
///
5153
/// This operation can cause undefined behavior if `name`'s data is invalid.
52-
NSTDAPI NSTDFile nstd_fs_file_open(const NSTDStr *name, NSTDUInt8 mask, NSTDIOError *errc);
54+
NSTDAPI NSTDFileResult nstd_fs_file_open(const NSTDStr *name, NSTDUInt8 mask);
5355

5456
/// Writes some data to a file & returns how many bytes were written.
5557
///

src/fs/file.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A handle to an opened file.
22
use crate::{
33
core::{
4+
result::NSTDResult,
45
slice::{NSTDSlice, NSTDSliceMut},
56
str::NSTDStr,
67
},
@@ -32,6 +33,9 @@ pub const NSTD_FILE_TRUNC: NSTDUInt8 = 1 << 4;
3233
/// A handle to an opened file.
3334
pub type NSTDFile = Box<File>;
3435

36+
/// A result type yielding an `NSTDFile` on success.
37+
pub type NSTDFileResult = NSTDResult<NSTDFile, NSTDIOError>;
38+
3539
/// Opens file on the filesystem and returns a handle to it.
3640
///
3741
/// # Parameters:
@@ -40,11 +44,9 @@ pub type NSTDFile = Box<File>;
4044
///
4145
/// - `NSTDUInt8 mask` - A bit mask for toggling the file's different open options.
4246
///
43-
/// - `NSTDIOError *errc` - Returns as the I/O operation error code.
44-
///
4547
/// # Returns
4648
///
47-
/// `NSTDFile file` - A handle to the opened file, or null if an error occurs.
49+
/// `NSTDFileResult file` - A handle to the opened file, or the IO error on failure.
4850
///
4951
/// # Panics
5052
///
@@ -54,11 +56,7 @@ pub type NSTDFile = Box<File>;
5456
///
5557
/// This operation can cause undefined behavior if `name`'s data is invalid.
5658
#[cfg_attr(feature = "clib", no_mangle)]
57-
pub unsafe extern "C" fn nstd_fs_file_open(
58-
name: &NSTDStr,
59-
mask: NSTDUInt8,
60-
errc: &mut NSTDIOError,
61-
) -> Option<NSTDFile> {
59+
pub unsafe extern "C" fn nstd_fs_file_open(name: &NSTDStr, mask: NSTDUInt8) -> NSTDFileResult {
6260
// Attempt to create/open the file in write mode.
6361
match File::options()
6462
.create((mask & NSTD_FILE_CREATE) != 0)
@@ -68,14 +66,8 @@ pub unsafe extern "C" fn nstd_fs_file_open(
6866
.truncate((mask & NSTD_FILE_TRUNC) != 0)
6967
.open(name.as_str())
7068
{
71-
Ok(f) => {
72-
*errc = NSTDIOError::NSTD_IO_ERROR_NONE;
73-
Some(Box::new(f))
74-
}
75-
Err(err) => {
76-
*errc = NSTDIOError::from_err(err.kind());
77-
None
78-
}
69+
Ok(f) => NSTDResult::Ok(Box::new(f)),
70+
Err(err) => NSTDResult::Err(NSTDIOError::from_err(err.kind())),
7971
}
8072
}
8173

0 commit comments

Comments
 (0)