Skip to main content

Request

Trait Request 

Source
pub trait Request {
    type Output;
    type Error;

    // Required method
    fn perform(&self) -> Result<Self::Output, Self::Error>;
}
Expand description

A request that may be performed more than once.

Implemented by the entries that carry parameters and produce something new each time: crate::open::OpenFile, crate::open_by_id::OpenFileByIdentifier, and crate::watch::WatchDirectory.

§Example

A consumer writes its own code against the trait, then tests it against a fake that never touches the filesystem:

use windows_namespace_request_sys::outcome::Outcome;
use windows_namespace_request_sys::request::Request;
use windows_namespace_request_sys::Win32Error;
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;

// The consumer's code: generic over the request, so it can be exercised
// without opening anything.
fn count_successes<R: Request>(requests: &[R], attempts: usize) -> usize {
    requests
        .iter()
        .flat_map(|request| (0..attempts).map(move |_| request.perform()))
        .filter(Result::is_ok)
        .count()
}

// The consumer's fake: a canned outcome, no Win32 anywhere.
struct AlwaysMissing;

impl Request for AlwaysMissing {
    type Error = Win32Error;
    type Output = ();

    fn perform(&self) -> Outcome<()> {
        Err(Win32Error::from_code(ERROR_FILE_NOT_FOUND))
    }
}

struct AlwaysOpens;

impl Request for AlwaysOpens {
    type Error = Win32Error;
    type Output = u32;

    fn perform(&self) -> Outcome<u32> {
        Ok(7)
    }
}

assert_eq!(count_successes(&[AlwaysMissing, AlwaysMissing], 3), 0);
assert_eq!(count_successes(&[AlwaysOpens], 3), 3, "a request may be performed repeatedly");

Required Associated Types§

Source

type Output

What performing the request produces.

Source

type Error

How performing it can fail.

Win32Error for every entry that fails only as Windows failed. The two that also retry a growing buffer – QueryFinalPath and ResolveFullPath – carry their own error instead, as the module documentation explains.

Required Methods§

Source

fn perform(&self) -> Result<Self::Output, Self::Error>

Performs the request on the calling thread.

§Errors

Returns the raw Win32 code, unaltered, per this crate’s faithful-execution contract – or, for an entry with a failure Win32 has no code for, that entry’s own error.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§