Skip to content

Commit

Permalink
fix: apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Feb 1, 2022
1 parent e2cfb4a commit ee1aa49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
25 changes: 9 additions & 16 deletions crates/libs/windows/src/core/agile_reference.rs
Expand Up @@ -2,27 +2,20 @@ use super::*;
use bindings::*;
use core::marker::PhantomData;

/// A safe wrapper around WinRT interfaces.
///
/// Some interfaces are not marked as agile thus then don't implement [`Send`].
/// These interfaces can be made agile through this `AgileReference`.
pub struct AgileReference<T> {
reference: IAgileReference,
_marker: PhantomData<T>,
}
/// A type representing an agile reference to a COM/WinRT object.
#[repr(transparent)]
#[derive(Clone, PartialEq, Eq)]
pub struct AgileReference<T>(IAgileReference, PhantomData<T>);

impl<T: Interface> AgileReference<T> {
/// Creates a new wrapper around the reference
pub fn new<'a>(from_ref: &'a T) -> Result<Self>
where
&'a T: IntoParam<'a, IUnknown>,
{
unsafe { RoGetAgileReference(AGILEREFERENCE_DEFAULT, &T::IID, from_ref).map(|reference| Self { reference, _marker: Default::default() }) }
/// Creates an agile reference to the object.
pub fn new<'a, O: IntoParam<'a, IUnknown>>(object: O) -> Result<Self> {
unsafe { RoGetAgileReference(AGILEREFERENCE_DEFAULT, &T::IID, object).map(|reference| Self(reference, Default::default())) }
}

/// Resolves the reference for the current thread.
/// Retrieves a proxy to the target of the `AgileReference` object that may safely be used within any thread context in which get is called.
pub fn resolve(&self) -> Result<T> {
unsafe { self.reference.Resolve() }
unsafe { self.0.Resolve() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tests/agile_reference/tests/tests.rs
Expand Up @@ -4,7 +4,7 @@ use windows::Media::Control::GlobalSystemMediaTransportControlsSessionManager;
#[test]
fn test() {
let manager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().unwrap().get().unwrap();
let reference = AgileReference::new(&manager).unwrap();
let reference = AgileReference::<GlobalSystemMediaTransportControlsSessionManager>::new(&manager).unwrap();

let handle = std::thread::spawn(move || {
let manager = reference.resolve().unwrap();
Expand Down

0 comments on commit ee1aa49

Please sign in to comment.