Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! Cross-platform interface to the `errno` variable.
2 //!
3 //! # Examples
4 //! ```
5 //! use errno::{Errno, errno, set_errno};
6 //!
7 //! // Get the current value of errno
8 //! let e = errno();
9 //!
10 //! // Set the current value of errno
11 //! set_errno(e);
12 //!
13 //! // Extract the error code as an i32
14 //! let code = e.0;
15 //!
16 //! // Display a human-friendly error message
17 //! println!("Error {}: {}", code, e);
18 //! ```
19
20 #![cfg_attr(not(feature = "std"), no_std)]
21
22 #[cfg_attr(unix, path = "unix.rs")]
23 #[cfg_attr(windows, path = "windows.rs")]
24 #[cfg_attr(target_os = "wasi", path = "wasi.rs")]
25 #[cfg_attr(target_os = "hermit", path = "hermit.rs")]
26 mod sysrust-analyzer cargo errno 0.3.14 sys/;
27
28 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
29 #[cfg(feature = "std")]
30 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#;
31 #[cfg(feature = "std")]
32 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/;
33
34 /// Wraps a platform-specific error code.
35 ///
36 /// The `Display` instance maps the code to a human-readable string. It
37 /// calls [`strerror_r`][1] under POSIX, and [`FormatMessageW`][2] on
38 /// Windows.
39 ///
40 /// [1]: http://pubs.opengroup.org/onlinepubs/009695399/functions/strerror.html
41 /// [2]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms679351%28v=vs.85%29.aspx
42 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
43 pub struct Errnorust-analyzer cargo errno 0.3.14 Errno#(pub i32);
44
45 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for Errnorust-analyzer cargo errno 0.3.14 Errno# {
46 fn fmtrust-analyzer cargo errno 0.3.14 impl#[Errno][Debug]fmt().(&self, fmt: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
47 sysrust-analyzer cargo errno 0.3.14 sys/::with_descriptionrust-analyzer cargo errno 0.3.14 sys/with_description().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self, |desc| {
48 fmt.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("Errno")
49 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("code", &self.0rust-analyzer cargo errno 0.3.14 Errno#0.)
50 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("description", &desc.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().())
51 .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().()
52 })
53 }
54 }
55
56 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errnorust-analyzer cargo errno 0.3.14 Errno# {
57 fn fmtrust-analyzer cargo errno 0.3.14 impl#[Errno][Display]fmt().(&self, fmt: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
58 sysrust-analyzer cargo errno 0.3.14 sys/::with_descriptionrust-analyzer cargo errno 0.3.14 sys/with_description().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self, |desc| match desc {
59 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(desc) => fmt.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(desc),
60 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(fm_err) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
61 fmt,
62 "OS error {} ({} returned error {})",
63 self.0rust-analyzer cargo errno 0.3.14 Errno#0.,
64 sysrust-analyzer cargo errno 0.3.14 sys/::STRERROR_NAMErust-analyzer cargo errno 0.3.14 sys/STRERROR_NAME.,
65 fm_err.0rust-analyzer cargo errno 0.3.14 Errno#0.
66 ),
67 })
68 }
69 }
70
71 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Errnorust-analyzer cargo errno 0.3.14 Errno#> for i32 {
72 fn fromrust-analyzer cargo errno 0.3.14 impl#[i32][`From<Errno>`]from().(e: Errnorust-analyzer cargo errno 0.3.14 Errno#) -> Selfrust-analyzer cargo errno 0.3.14 impl#[i32][`From<Errno>`] {
73 e.0rust-analyzer cargo errno 0.3.14 Errno#0.
74 }
75 }
76
77 #[cfg(feature = "std")]
78 impl Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errnorust-analyzer cargo errno 0.3.14 Errno# {
79 // TODO: Remove when MSRV >= 1.27
80 #[allow(deprecated)]
81 fn descriptionrust-analyzer cargo errno 0.3.14 impl#[Errno][Error]description().(&self) -> &str {
82 "system error"
83 }
84 }
85
86 #[cfg(feature = "std")]
87 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Errnorust-analyzer cargo errno 0.3.14 Errno#> for iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# {
88 fn fromrust-analyzer cargo errno 0.3.14 impl#[`io::Error`][`From<Errno>`]from().(errno: Errnorust-analyzer cargo errno 0.3.14 Errno#) -> Selfrust-analyzer cargo errno 0.3.14 impl#[`io::Error`][`From<Errno>`] {
89 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::from_raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]from_raw_os_error().(errno.0rust-analyzer cargo errno 0.3.14 Errno#0.)
90 }
91 }
92
93 /// Returns the platform-specific value of `errno`.
94 pub fn errnorust-analyzer cargo errno 0.3.14 errno().() -> Errnorust-analyzer cargo errno 0.3.14 Errno# {
95 sysrust-analyzer cargo errno 0.3.14 sys/::errnorust-analyzer cargo errno 0.3.14 sys/errno().()
96 }
97
98 /// Sets the platform-specific value of `errno`.
99 pub fn set_errnorust-analyzer cargo errno 0.3.14 set_errno().(err: Errnorust-analyzer cargo errno 0.3.14 Errno#) {
100 sysrust-analyzer cargo errno 0.3.14 sys/::set_errnorust-analyzer cargo errno 0.3.14 sys/set_errno().(err)
101 }
102
103 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
104 fn it_worksrust-analyzer cargo errno 0.3.14 it_works().() {
105 let x = errnorust-analyzer cargo errno 0.3.14 errno().();
106 set_errnorust-analyzer cargo errno 0.3.14 set_errno().(x);
107 }
108
109 #[cfg(feature = "std")]
110 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
111 fn it_works_with_to_stringrust-analyzer cargo errno 0.3.14 it_works_with_to_string().() {
112 let x = errnorust-analyzer cargo errno 0.3.14 errno().();
113 let _ = x.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
114 }
115
116 #[cfg(feature = "std")]
117 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
118 fn check_descriptionrust-analyzer cargo errno 0.3.14 check_description().() {
119 let expect = if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(windows) {
120 "Incorrect function."
121 } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "illumos") {
122 "Not owner"
123 } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "wasi") || cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "emscripten") {
124 "Argument list too long"
125 } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "haiku") {
126 "Operation not allowed"
127 } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "vxworks") {
128 "operation not permitted"
129 } else {
130 "Operation not permitted"
131 };
132
133 let errno_code = if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "haiku") {
134 -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().2147483633
135 } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "hurd") {
136 1073741825
137 } else {
138 1
139 };
140 set_errnorust-analyzer cargo errno 0.3.14 set_errno().(Errnorust-analyzer cargo errno 0.3.14 Errno#(errno_code));
141
142 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(errnorust-analyzer cargo errno 0.3.14 errno().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), expect);
143 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
144 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", errnorust-analyzer cargo errno 0.3.14 errno().()),
145 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
146 "Errno {{ code: {}, description: Some({:?}) }}",
147 errno_code, expect
148 )
149 );
150 }
151
152 #[cfg(feature = "std")]
153 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
154 fn check_error_into_errnorust-analyzer cargo errno 0.3.14 check_error_into_errno().() {
155 const ERROR_CODErust-analyzer cargo errno 0.3.14 ERROR_CODE.: i32 = 1;
156
157 let error = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::from_raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]from_raw_os_error().(ERROR_CODErust-analyzer cargo errno 0.3.14 ERROR_CODE.);
158 let new_error: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# = Errnorust-analyzer cargo errno 0.3.14 Errno#(ERROR_CODErust-analyzer cargo errno 0.3.14 ERROR_CODE.).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
159 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(error.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), new_error.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().());
160 }