Skip to main content

ChangeNotification

Struct ChangeNotification 

Source
pub struct ChangeNotification { /* private fields */ }
Expand description

An owned change-notification handle.

Closed with FindCloseChangeNotification on drop. The type exists so that the close routine travels with the handle rather than being something each call site has to remember, which is the same shape windows-threadpool-sys already needed for wait targets.

Implementations§

Source§

impl ChangeNotification

Source

pub fn as_raw(&self) -> HANDLE

The raw handle, for passing to a wait.

Borrowed, not transferred: it stays owned by this value. Do not pass it to CloseHandle.

Source

pub fn rearm(&self) -> Outcome<()>

Rearms the watch after it has signalled.

A notification handle signals once and then stays signalled until it is rearmed, so a consumer that waits in a loop must call this between waits.

§Errors

Returns the raw Win32 code, unaltered.

§Example

Without the rearm, the second wait returns immediately on the first change forever, and a consumer’s loop spins:

use std::fs;

use windows_namespace_request_sys::prepare;
use windows_namespace_request_sys::watch::{NotifyFilter, WatchDirectory};
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
use windows_sys::Win32::System::Threading::WaitForSingleObject;
use wtf_string::Wtf16String;

let directory = std::env::temp_dir().join(format!("wnrs-rearm-{}", std::process::id()));
let _ = fs::remove_dir_all(&directory);
fs::create_dir_all(&directory)?;

let text = directory.to_str().expect("a temporary path is valid UTF-8");
let notification = WatchDirectory::new(prepare(&Wtf16String::from(text))?)
    .with_filter(NotifyFilter::FILE_NAME)
    .perform()?;

fs::write(directory.join("first.t"), b"x")?;
// SAFETY: the handle is live for the notification's lifetime.
assert_eq!(unsafe { WaitForSingleObject(notification.as_raw(), 5_000) }, WAIT_OBJECT_0);

// The handle stays signalled until this runs.
notification.rearm()?;

fs::write(directory.join("second.t"), b"x")?;
// SAFETY: as above.
assert_eq!(unsafe { WaitForSingleObject(notification.as_raw(), 5_000) }, WAIT_OBJECT_0);

Trait Implementations§

Source§

impl Debug for ChangeNotification

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for ChangeNotification

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for ChangeNotification

Source§

impl Sync for ChangeNotification

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.