-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
542 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub trait CanInto<T>: Sized { | ||
const QUERY: bool = false; | ||
} | ||
|
||
impl<T> CanInto<T> for T where T: Clone {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use super::*; | ||
|
||
/// Provides automatic parameter conversion in cases where the Windows API expects implicit conversion support. | ||
/// | ||
/// This is a mutable version of [Param] meant to support out parameters. | ||
/// There is no need to implement this trait. Blanket implementations are provided for all applicable Windows types. | ||
pub trait ParamMut<T: TypeKind, C = <T as TypeKind>::TypeKind>: Sized | ||
where | ||
T: Type<T>, | ||
{ | ||
#[doc(hidden)] | ||
unsafe fn borrow_mut(&self) -> RefMut<'_, T>; | ||
} | ||
|
||
impl<T> ParamMut<T, CloneType> for &mut T | ||
where | ||
T: TypeKind<TypeKind = CloneType> + Clone + Default, | ||
{ | ||
unsafe fn borrow_mut(&self) -> RefMut<'_, T> { | ||
let this: &mut T = std::mem::transmute_copy(self); | ||
std::mem::take(this); | ||
std::mem::transmute_copy(self) | ||
} | ||
} | ||
|
||
impl<T> ParamMut<T, CopyType> for &mut T | ||
where | ||
T: TypeKind<TypeKind = CopyType> + Clone + Default, | ||
{ | ||
unsafe fn borrow_mut(&self) -> RefMut<'_, T> { | ||
std::mem::transmute_copy(self) | ||
} | ||
} | ||
|
||
impl<T> ParamMut<T, InterfaceType> for &mut Option<T> | ||
where | ||
T: TypeKind<TypeKind = InterfaceType> + Clone, | ||
{ | ||
unsafe fn borrow_mut(&self) -> RefMut<'_, T> { | ||
let this: &mut Option<T> = std::mem::transmute_copy(self); | ||
std::mem::take(this); | ||
std::mem::transmute_copy(self) | ||
} | ||
} | ||
|
||
impl<T> ParamMut<T> for Option<&mut T> | ||
where | ||
T: Type<T>, | ||
{ | ||
unsafe fn borrow_mut(&self) -> RefMut<'_, T> { | ||
match self { | ||
Some(this) => std::mem::transmute_copy(this), | ||
None => std::mem::zeroed(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use super::*; | ||
|
||
#[doc(hidden)] | ||
pub enum ParamValue<T: Type<T>> { | ||
Owned(T), | ||
Borrowed(T::Abi), | ||
} | ||
|
||
impl<T: Type<T>> ParamValue<T> { | ||
// TODO: replace with `borrow` in windows-bindgen | ||
pub fn abi(&self) -> T::Abi { | ||
unsafe { | ||
match self { | ||
Self::Owned(item) => std::mem::transmute_copy(item), | ||
Self::Borrowed(borrowed) => std::mem::transmute_copy(borrowed), | ||
} | ||
} | ||
} | ||
|
||
pub fn borrow(&self) -> Ref<'_, T> { | ||
unsafe { std::mem::transmute_copy(&self.abi()) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use super::*; | ||
|
||
/// A borrowed type with the same memory layout as the type itself that can be used to construct ABI-compatible function signatures. | ||
#[repr(transparent)] | ||
pub struct Ref<'a, T: Type<T>>(T::Abi, std::marker::PhantomData<&'a T>); | ||
|
||
impl<'a, T: Type<T>> Ref<'a, T> { | ||
/// Reads the borrowed value. | ||
pub fn read(&self) -> &T::Default { | ||
unsafe { std::mem::transmute(&self.0) } | ||
} | ||
|
||
/// Clones the borrowed value. | ||
pub fn ok(&self) -> Result<T> { | ||
T::from_default(self.read()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use super::*; | ||
|
||
/// A borrowed type with the same memory layout as the type itself that can be used to construct ABI-compatible function signatures. | ||
/// | ||
/// This is a mutable version of [Ref] meant to support out parameters. | ||
#[repr(transparent)] | ||
pub struct RefMut<'a, T: Type<T>>(*mut T::Abi, std::marker::PhantomData<&'a T>); | ||
|
||
impl<'a, T: Type<T>> RefMut<'a, T> { | ||
/// Returns `true` if the argument is null. | ||
pub fn is_null(&self) -> bool { | ||
self.0.is_null() | ||
} | ||
|
||
/// Overwrites a memory location with the given value without reading or dropping the old value. | ||
pub fn write(self, value: T::Default) -> Result<()> { | ||
if self.0.is_null() { | ||
Err(Error::from_hresult(imp::E_POINTER)) | ||
} else { | ||
unsafe { *self.0 = std::mem::transmute_copy(&value) } | ||
std::mem::forget(value); | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.