Skip to content

Commit

Permalink
AsImpl::as_impl should be unsafe (#2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jul 4, 2023
1 parent fa7b524 commit 7b424c2
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
6 changes: 5 additions & 1 deletion crates/libs/core/src/as_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
///
/// This trait is automatically implemented when using the `implement` macro.
pub trait AsImpl<T> {
fn as_impl(&self) -> &T;
/// # Safety
///
/// The caller needs to ensure that `self` is actually implemented by the
/// implementation `T`.
unsafe fn as_impl(&self) -> &T;
}
3 changes: 3 additions & 0 deletions crates/libs/core/src/imp/weak_ref_count.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// TODO: looks like a bug in nightly clippy as the suggestion is invalid.
#![allow(clippy::unnecessary_cast)]

use super::*;
use crate::ComInterface;
use std::sync::atomic::{AtomicIsize, Ordering};
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/implement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
}
}
impl #generics ::windows::core::AsImpl<#original_ident::#generics> for #interface_ident where #constraints {
fn as_impl(&self) -> &#original_ident::#generics {
unsafe fn as_impl(&self) -> &#original_ident::#generics {
let this = ::windows::core::Interface::as_raw(self);
// SAFETY: the offset is guranteed to be in bounds, and the implementation struct
// is guaranteed to live at least as long as `self`.
Expand Down
16 changes: 8 additions & 8 deletions crates/libs/windows/src/Windows/Foundation/Collections/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ where
<T as ::windows_core::Type<T>>::Default: ::std::clone::Clone,
{
fn Current(&self) -> ::windows_core::Result<T> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if owner.values.len() > current {
Expand All @@ -772,14 +772,14 @@ where
}

fn HasCurrent(&self) -> ::windows_core::Result<bool> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

Ok(owner.values.len() > current)
}

fn MoveNext(&self) -> ::windows_core::Result<bool> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if current < owner.values.len() {
Expand All @@ -790,7 +790,7 @@ where
}

fn GetMany(&self, values: &mut [T::Default]) -> ::windows_core::Result<u32> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

let actual = std::cmp::min(owner.values.len() - current, values.len());
Expand Down Expand Up @@ -1034,7 +1034,7 @@ where
<T as ::windows_core::Type<T>>::Default: std::clone::Clone + std::cmp::PartialEq,
{
fn Current(&self) -> ::windows_core::Result<T> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if owner.values.len() > current {
Expand All @@ -1045,14 +1045,14 @@ where
}

fn HasCurrent(&self) -> ::windows_core::Result<bool> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

Ok(owner.values.len() > current)
}

fn MoveNext(&self) -> ::windows_core::Result<bool> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if current < owner.values.len() {
Expand All @@ -1063,7 +1063,7 @@ where
}

fn GetMany(&self, values: &mut [T::Default]) -> ::windows_core::Result<u32> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

let actual = std::cmp::min(owner.values.len() - current, values.len());
Expand Down
5 changes: 1 addition & 4 deletions crates/tools/riddle/src/idl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,7 @@ impl Struct {
let mut fields = vec![];

let syn::Fields::Named(named) = item.fields else {
return Err(syn::Error::new(
item.span(),
"unnamed fields not supported",
));
return Err(syn::Error::new(item.span(), "unnamed fields not supported"));
};

for field in named.named {
Expand Down
2 changes: 1 addition & 1 deletion crates/tools/riddle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn canonicalize(path: &str) -> Result<String> {

let path = path
.to_string_lossy()
.trim_start_matches(r#"\\?\"#)
.trim_start_matches(r"\\?\")
.to_string();

match path.rsplit_once('.') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
<T as ::windows_core::Type<T>>::Default: ::std::clone::Clone,
{
fn Current(&self) -> ::windows_core::Result<T> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if owner.values.len() > current {
Expand All @@ -52,14 +52,14 @@ where
}

fn HasCurrent(&self) -> ::windows_core::Result<bool> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

Ok(owner.values.len() > current)
}

fn MoveNext(&self) -> ::windows_core::Result<bool> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if current < owner.values.len() {
Expand All @@ -71,7 +71,7 @@ where
}

fn GetMany(&self, values: &mut [T::Default]) -> ::windows_core::Result<u32> {
let owner: &StockIterable<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockIterable<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

let actual = std::cmp::min(owner.values.len() - current, values.len());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
<T as ::windows_core::Type<T>>::Default: std::clone::Clone + std::cmp::PartialEq,
{
fn Current(&self) -> ::windows_core::Result<T> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if owner.values.len() > current {
Expand All @@ -88,14 +88,14 @@ where
}

fn HasCurrent(&self) -> ::windows_core::Result<bool> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

Ok(owner.values.len() > current)
}

fn MoveNext(&self) -> ::windows_core::Result<bool> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

if current < owner.values.len() {
Expand All @@ -107,7 +107,7 @@ where
}

fn GetMany(&self, values: &mut [T::Default]) -> ::windows_core::Result<u32> {
let owner: &StockVectorView<T> = ::windows_core::AsImpl::as_impl(&self.owner);
let owner: &StockVectorView<T> = unsafe { ::windows_core::AsImpl::as_impl(&self.owner) };
let current = self.current.load(::std::sync::atomic::Ordering::Relaxed);

let actual = std::cmp::min(owner.values.len() - current, values.len());
Expand Down
7 changes: 6 additions & 1 deletion crates/tools/riddle/src/rust/try_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ pub fn try_format(writer: &super::Writer, tokens: &str) -> String {
{allow}{tokens}"#
);

let Ok(mut child) = std::process::Command::new("rustfmt").stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn() else {
let Ok(mut child) = std::process::Command::new("rustfmt")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null())
.spawn()
else {
return tokens;
};

Expand Down

0 comments on commit 7b424c2

Please sign in to comment.