1
1
//! A handle to an opened file.
2
2
use crate :: {
3
3
core:: {
4
+ result:: NSTDResult ,
4
5
slice:: { NSTDSlice , NSTDSliceMut } ,
5
6
str:: NSTDStr ,
6
7
} ,
@@ -32,6 +33,9 @@ pub const NSTD_FILE_TRUNC: NSTDUInt8 = 1 << 4;
32
33
/// A handle to an opened file.
33
34
pub type NSTDFile = Box < File > ;
34
35
36
+ /// A result type yielding an `NSTDFile` on success.
37
+ pub type NSTDFileResult = NSTDResult < NSTDFile , NSTDIOError > ;
38
+
35
39
/// Opens file on the filesystem and returns a handle to it.
36
40
///
37
41
/// # Parameters:
@@ -40,11 +44,9 @@ pub type NSTDFile = Box<File>;
40
44
///
41
45
/// - `NSTDUInt8 mask` - A bit mask for toggling the file's different open options.
42
46
///
43
- /// - `NSTDIOError *errc` - Returns as the I/O operation error code.
44
- ///
45
47
/// # Returns
46
48
///
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 .
48
50
///
49
51
/// # Panics
50
52
///
@@ -54,11 +56,7 @@ pub type NSTDFile = Box<File>;
54
56
///
55
57
/// This operation can cause undefined behavior if `name`'s data is invalid.
56
58
#[ 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 {
62
60
// Attempt to create/open the file in write mode.
63
61
match File :: options ( )
64
62
. create ( ( mask & NSTD_FILE_CREATE ) != 0 )
@@ -68,14 +66,8 @@ pub unsafe extern "C" fn nstd_fs_file_open(
68
66
. truncate ( ( mask & NSTD_FILE_TRUNC ) != 0 )
69
67
. open ( name. as_str ( ) )
70
68
{
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 ( ) ) ) ,
79
71
}
80
72
}
81
73
0 commit comments