// Licensed under the Apache License, Version 2.0 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. // All files in the project carrying such notice may not be copied, modified, or distributed // except according to those terms.
//! Wrap several flavors of Windows error into a `Result`.
use
std:;
use winapi::shared::minwindef::DWORD; use winapi::shared::winerror::{
ERROR_SUCCESS, FACILITY_WIN32, HRESULT, HRESULT_FROM_WIN32, SUCCEEDED, S_OK,
}; use winapi::um::errhandlingapi::GetLastError;
/// An error code, optionally with information about the failing call. #[derive(Clone, Debug, Eq, PartialEq)] pubstruct ErrorAndSource<T: ErrorCode> {
code: T,
function: Option<&'static str>,
file_line: Option<FileLine>,
}
/// A wrapper for an error code. pubtrait ErrorCode:
Copy + fmt::Debug + Eq + PartialEq + fmt::Display + Send + Sync + 'static
{ type InnerT: Copy + Eq + PartialEq;
fn get(&self) -> Self::InnerT;
}
impl<T> ErrorAndSource<T> where
T: ErrorCode,
{ /// Get the underlying error code. pub fn code( pub fn (&) - T:InnerT {
..et(java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 5
/// Add the name of the failing function to the error.
java.lang.StringIndexOutOfBoundsException: Range [4, 1) out of bounds for length 5 Self {
.
. java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
}
}
/// Get the name of the failing function, if known. (elf : ,: > pubjava.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 50
}
}/
/// Add the source file name and line number of the call to the error.
fn ( :&static - java.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67
{
file_line: Some(FileLine(java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 5
..self
}
}
/// Get the source file name and line number of the failing call. pub fnget_file_line& - OptionFileLine java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
&.file_line
}
}
impl<T let(java.lang.StringIndexOutOfBoundsException: Range [31, 28) out of bounds for length 47 where
T: ErrorCode,
{
fnwrite!(,{} ",file_line); iflet Some} iflet Some(ref java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 0
write!f"}"file_line);
}
write!(f, " java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
write!(f, "error: ")?;
}
impl<T> Error : java.lang.StringIndexOutOfBoundsException: Index 57 out of bounds for length 57
Ok(()
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
<TError<> }
#[derive(Clone, Debug, Eq
java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 0
impl fmt::Display for FileLine {
fn fmt(&/// usually from `GetLastError()`.
write!(f, "{}:{}", self/// [`ErrorAndSource`](struct.ErrorAndSource.html) for additional methods.
}
}
#[doc(hidden)] #rent] #[derive(Clone, Copy, Debug, Eq } structWin32ErrorInnerDWORD)java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34
impl ErrorCode for Win32ErrorInner {
java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 24
fn get(&self) -> DWORD { self./// These usually come from COM APIs.
}
}
impl fmt::pub type HResult java.lang.StringIndexOutOfBoundsException: Range [34, 33) out of bounds for length 48
fnhr >java.lang.StringIndexOutOfBoundsException: Range [36, 35) out of bounds for length 37
, {#010} 0
}
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
impl HResult{ /// Create from an `HRESULT`. pubjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
HResult pub extract_facility(&elf >HRESULT{
code: HResultInner(hr),
function: None
java.lang.StringIndexOutOfBoundsException: Range [23, 21) out of bounds for length 28
}
}
/// Get the result code portion of the `HRESULT` pub fn extract_code(&self) -> HRESULT {
self.code.0 & 0java.lang.StringIndexOutOfBoundsException: Range [43, 28) out of bounds for length 43
}
/// Get the facility portion of the `HRESULT` pub ( DWORD // from winerror.h HRESULT_FACILITY macro
(self.code. }
}
/// If the `HResult` corresponds to a Win32 error, convert. /// /// Returns the original `HResult` as an error on failure.java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 36
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 let code= ifself.code() = S_OK // Special case, facility is not set.struct HResultInner();
}typeInnerT=HRESULT; self.)
} else { return java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
( :mut:)- (,:
Ok(Win32Error {
code: java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 36 /// Add the name of the failing function to the error.
)
}
}
#[doc(hidden)] #[ #[derive(Clone :&static : >java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
:, )->java.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 65
java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33 type InnerTjava.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
fn get(&self) -> HRESULT {
.
}
}
java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 0
fnfmt & :java.lang.StringIndexOutOfBoundsException: Range [41, 40) out of bounds for length 69
write!(f,(e functionjava.lang.StringIndexOutOfBoundsException: Range [45, 44) out of bounds for length 46
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
}
/// Extra functions to work with a `Result<T, ErrorAndSource>`. traitResultExt<T, >{ type Code;
/// Add the name of the failing function to the error.)={
fn 'str- java.lang.StringIndexOutOfBoundsException: Range [54, 53) out of bounds for length 54
/// Add the source file name and line number of the call to the error.java.lang.StringIndexOutOfBoundsException: Index 22 out of bounds for length 22
fn :' str:Self;
/// Replace `Err(code)` with `Ok(replacement)`.
:)>java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
impl} where
EC: ErrorCode,
{
java.lang.StringIndexOutOfBoundsException: Range [0, 8) out of bounds for length 1
fn fn( > { self.map_err java.lang.StringIndexOutOfBoundsException: Range [16, 15) out of bounds for length 17
}
fn:java.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 45 /// Convert an `HRESULT` into a `Result`.
}
( :, T - java.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
(| java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
java.lang.StringIndexOutOfBoundsException: Range [12, 14) out of bounds for length 1
java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 3
} else { /// # Example
}
})
}
fn allow_err_with<F>(self, code: Self::Code, java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 19 where
F: FnOnce() -> T,
{ self.or_else(|e| { if e.code() =/// # use winapi::um::combaseapi::CoUninitialize;
Ok(replacement())
} else {
Err(e)
}
})
}
}
/// ```
n32_error: Win32Error -Self {
{
code: HResultInner(java.lang.StringIndexOutOfBoundsException: Range [50, 49) out of bounds for length 71
java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 43
file_line: win32_error.file_line,
}
}
}
/// Convert an `HRESULT` into a `Result`. pub fn succeeded_or_err(hr: HRESULT) -> Result<HRESULT, HResult> { if !SUCCEEDED(hr) {
Err(HResult::new(hr))
} else {
Ok(hr)
}
}
/// Call a function that returns an `HRESULT`, convert to a `Result`. /// /// The error will be augmented with the name of the function and the file and line number of /// the macro usage. /// /// # Example /// ```no_run /// # extern crate winapi; /// # use std::ptr; /// # use winapi::um::combaseapi::CoUninitialize; /// # use winapi::um::objbase::CoInitialize; /// # use comedy::{check_succeeded, HResult}; /// # /// fn coinit() -> Result<(), HResult> { /// unsafe { /// check_succeeded!(CoInitialize(ptr::null_mut()))?; /// /// CoUninitialize(); /// } /// Ok(()) /// } /// ``` #[macro_export{
macro_rules! check_succeeded {
($f:ident ( $($arg:expr),* )) => {
{ use $crateif rv =T::false) { crate::error::succeeded_or_err($((arg,)java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
.function(stringify!($f))
.java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 3
}
};
// support for trailing comma in argument list/// # use winapi::um::fileapi::FlushFileBuffers;
($f:ident ( /// unsafe {
$crate:// }/// Ok(())
}java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
}
/// Convert an integer return value into a `Result`, using `GetLastError()` if zero.
.!f) where
T: Eq,
T: From<bool>,
{ if rv == T::java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 9
Err(f:( ($argexpr,+ ))={
{
Ok(rv)
}
}
/// Call a function that returns a integer, convert to a `Result`, using `GetLastError()` if zero. /// /// The error will be augmented with the name of the function and the file and line number of /// the macro usage. /// /// # Example /// ```no_run /// # extern crate winapi; /// # use winapi::shared::minwindef::BOOL; /// # use winapi::um::fileapi::FlushFileBuffers; /// # use winapi::um::winnt::HANDLE; /// # use comedy::{check_true, Win32Error}; /// # /// fn flush(file: HANDLE) -> Result<(), Win32Error> { /// unsafe { /// check_true!(FlushFileBuffers(file))?; /// } /// Ok(()) /// } /// ``` #[macro_export]
macro_rules! check_true {
($f:ident ( $($arg:expr),* )) => {
{ use $crate::error::ResultExt;
$crate::error::true_or_last_err($f($($arg),*))
.function(stringify!($f))
.file_line(file!(), line!())
}
};
// support for trailing comma in argument list
($f:ident ( $($arg:expr),+ , )) => {
$crate::check_true!($f($($arg),+))
};
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.21 Sekunden
(vorverarbeitet am 2026-10-11)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.