Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Jul 19, 2023
1 parent 39aaea4 commit 81588ec
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 45 deletions.
24 changes: 14 additions & 10 deletions src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<T: Interface> ComPtr<T> {
/// - if `raw` is not null, it must be a valid pointer to a COM object that implements T.
pub unsafe fn from_raw(raw: *mut T) -> Self {
if !raw.is_null() {
(&*(raw as *mut IUnknown)).AddRef();
(*(raw as *mut IUnknown)).AddRef();
}
ComPtr(raw)
}
Expand All @@ -48,7 +48,7 @@ impl<T: Interface> ComPtr<T> {
/// This is useful when D3D functions initialize objects by filling in a pointer to pointer
/// by taking `void**` as an argument.
///
/// # Safety:
/// # Safety
///
/// - Any modifications done to this pointer must result in the pointer either:
/// - being set to null
Expand All @@ -59,7 +59,7 @@ impl<T: Interface> ComPtr<T> {
// end up with a reference to a temporary.
let refer: &mut *mut T = &mut self.0;
let void: *mut *mut c_void = refer.cast();

// SAFETY: This reference is valid for the duration of the borrow due our mutable borrow of self.
&mut *void
}
Expand All @@ -69,7 +69,7 @@ impl<T: Interface> ComPtr<T> {
/// This is useful when D3D functions initialize objects by filling in a pointer to pointer
/// by taking `T**` as an argument.
///
/// # Safety:
/// # Safety
///
/// - Any modifications done to this pointer must result in the pointer either:
/// - being set to null
Expand All @@ -82,7 +82,7 @@ impl<T: Interface> ComPtr<T> {
impl<T: Interface> ComPtr<T> {
/// Returns a reference to the inner pointer casted as a pointer IUnknown.
///
/// # Safety:
/// # Safety
///
/// - This pointer must not be null.
pub unsafe fn as_unknown(&self) -> &IUnknown {
Expand All @@ -92,7 +92,7 @@ impl<T: Interface> ComPtr<T> {

/// Casts the T to U using QueryInterface.
///
/// # Safety:
/// # Safety
///
/// - This pointer must not be null.
pub unsafe fn cast<U>(&self) -> D3DResult<ComPtr<U>>
Expand Down Expand Up @@ -269,12 +269,16 @@ macro_rules! weak_com_inheritance_chain {
$variant:ident($type:ty);
$($next_variant:ident),*;
) => {
// Construct this enum from weak pointer to this interface. For best usability, always use the highest constructor you can. This doesn't try to upcast.
#[doc = concat!("Constructs this enum from a ComPtr to ", stringify!($variant), ". For best usability, always use the highest constructor you can. This doesn't try to upcast.")]
///
/// # Safety
///
#[doc = concat!(" - The value must be a valid pointer to a COM object that implements ", stringify!($variant))]
$vis unsafe fn $from_name(value: $crate::ComPtr<$type>) -> Self {
Self::$variant(value)
}

// Returns Some if the value implements the interface otherwise returns None.
#[doc = concat!("Returns Some if the value implements ", stringify!($variant), ".")]
$vis fn $as_name(&self) -> Option<&$crate::ComPtr<$type>> {
match *self {
$(
Expand All @@ -283,14 +287,14 @@ macro_rules! weak_com_inheritance_chain {
Self::$variant(ref v) => Some(v),
$(
Self::$next_variant(ref v) => {
// v is &ComPtr<NextType> and se cast to &ComPtr<Type>
// v is &ComPtr<NextType> and we cast to &ComPtr<Type>
Some(unsafe { std::mem::transmute(v) })
}
)*
}
}

// Returns the interface if the value implements it, otherwise panics.
#[doc = concat!("Returns a ", stringify!($variant), " if the value implements it, otherwise panics.")]
#[track_caller]
$vis fn $unwrap_name(&self) -> &$crate::ComPtr<$type> {
match *self {
Expand Down
2 changes: 1 addition & 1 deletion src/command_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum CmdListType {
// VideoProcess = d3d12::D3D12_COMMAND_LIST_TYPE_VIDEO_PROCESS,
}

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ClearFlags: u32 {
const DEPTH = d3d12::D3D12_CLEAR_FLAG_DEPTH;
Expand Down
15 changes: 5 additions & 10 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum DescriptorHeapType {
Dsv = d3d12::D3D12_DESCRIPTOR_HEAP_TYPE_DSV,
}

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DescriptorHeapFlags: u32 {
const SHADER_VISIBLE = d3d12::D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
Expand Down Expand Up @@ -253,7 +253,7 @@ pub enum RootSignatureVersion {
V1_1 = d3d12::D3D_ROOT_SIGNATURE_VERSION_1_1,
}

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RootSignatureFlags: u32 {
const ALLOW_IA_INPUT_LAYOUT = d3d12::D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
Expand Down Expand Up @@ -297,12 +297,7 @@ impl crate::D3D12Lib {
let mut error = Error::null();
let hr = unsafe {
let func: libloading::Symbol<Fun> = self.lib.get(b"D3D12SerializeRootSignature")?;
func(
&desc,
version as _,
blob.mut_void() as *mut *mut _,
error.mut_void() as *mut *mut _,
)
func(&desc, version as _, blob.mut_self(), error.mut_self())
};

Ok(((blob, error), hr))
Expand Down Expand Up @@ -332,8 +327,8 @@ impl RootSignature {
d3d12::D3D12SerializeRootSignature(
&desc,
version as _,
blob.mut_void() as *mut *mut _,
error.mut_void() as *mut *mut _,
blob.mut_self(),
error.mut_self(),
)
};

Expand Down
35 changes: 23 additions & 12 deletions src/dxgi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use winapi::{
Interface,
};

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct FactoryCreationFlags: u32 {
const DEBUG = dxgi1_3::DXGI_CREATE_FACTORY_DEBUG;
Expand Down Expand Up @@ -211,7 +211,10 @@ impl SwapchainDesc {
}

impl Factory1 {
pub fn create_swapchain(
/// # Safety
///
/// - `queue` must be a valid pointer to a D3D device.
pub unsafe fn create_swapchain(
&self,
queue: *mut IUnknown,
hwnd: HWND,
Expand Down Expand Up @@ -242,16 +245,18 @@ impl Factory1 {
};

let mut swapchain = SwapChain::null();
let hr =
unsafe { self.CreateSwapChain(queue, &mut desc, swapchain.mut_void() as *mut *mut _) };
let hr = unsafe { self.CreateSwapChain(queue, &mut desc, swapchain.mut_self()) };

(swapchain, hr)
}
}

impl Factory2 {
// TODO: interface not complete
pub fn create_swapchain_for_hwnd(
/// # Safety
///
/// - `queue` must be a valid pointer to a D3D device.
pub unsafe fn create_swapchain_for_hwnd(
&self,
queue: *mut IUnknown,
hwnd: HWND,
Expand All @@ -265,14 +270,17 @@ impl Factory2 {
&desc.to_desc1(),
ptr::null(),
ptr::null_mut(),
swap_chain.mut_void() as *mut *mut _,
swap_chain.mut_self(),
)
};

(swap_chain, hr)
}

pub fn create_swapchain_for_composition(
/// # Safety
///
/// - `queue` must be a valid pointer to a D3D device.
pub unsafe fn create_swapchain_for_composition(
&self,
queue: *mut IUnknown,
desc: &SwapchainDesc,
Expand All @@ -283,7 +291,7 @@ impl Factory2 {
queue,
&desc.to_desc1(),
ptr::null_mut(),
swap_chain.mut_void() as *mut *mut _,
swap_chain.mut_self(),
)
};

Expand All @@ -308,14 +316,17 @@ impl Factory4 {

pub fn enumerate_adapters(&self, id: u32) -> D3DResult<Adapter1> {
let mut adapter = Adapter1::null();
let hr = unsafe { self.EnumAdapters1(id, adapter.mut_void() as *mut *mut _) };
let hr = unsafe { self.EnumAdapters1(id, adapter.mut_self()) };

(adapter, hr)
}
}

impl FactoryMedia {
pub fn create_swapchain_for_composition_surface_handle(
/// # Safety
///
/// - `queue` must be a valid pointer to a D3D device.
pub unsafe fn create_swapchain_for_composition_surface_handle(
&self,
queue: *mut IUnknown,
surface_handle: HANDLE,
Expand All @@ -328,15 +339,15 @@ impl FactoryMedia {
surface_handle,
&desc.to_desc1(),
ptr::null_mut(),
swap_chain.mut_void() as *mut *mut _,
swap_chain.mut_self(),
)
};

(swap_chain, hr)
}
}

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct SwapChainPresentFlags: u32 {
const DXGI_PRESENT_DO_NOT_SEQUENCE = dxgi::DXGI_PRESENT_DO_NOT_SEQUENCE;
Expand Down
2 changes: 1 addition & 1 deletion src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum MemoryPool {
L1 = d3d12::D3D12_MEMORY_POOL_L1,
}

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct HeapFlags: u32 {
const NONE = d3d12::D3D12_HEAP_FLAG_NONE;
Expand Down
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#[macro_use]
extern crate bitflags;
#![allow(clippy::too_many_arguments)]

use std::{convert::TryFrom, ffi::CStr};
use winapi::{
Expand Down Expand Up @@ -100,10 +99,12 @@ pub type Blob = ComPtr<d3dcommon::ID3DBlob>;

pub type Error = ComPtr<d3dcommon::ID3DBlob>;
impl Error {
pub unsafe fn as_c_str(&self) -> &CStr {
debug_assert!(!self.is_null());
let data = self.GetBufferPointer();
CStr::from_ptr(data as *const _ as *const _)
pub fn as_c_str(&self) -> &CStr {
assert!(!self.is_null());
unsafe {
let data = self.GetBufferPointer();
CStr::from_ptr(data as *const _ as *const _)
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use std::{
};
use winapi::um::{d3d12, d3dcompiler};

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PipelineStateFlags: u32 {
const TOOL_DEBUG = d3d12::D3D12_PIPELINE_STATE_FLAG_TOOL_DEBUG;
}
}

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ShaderCompileFlags: u32 {
const DEBUG = d3dcompiler::D3DCOMPILE_DEBUG;
Expand Down Expand Up @@ -86,8 +86,8 @@ impl<'a> Shader<'a> {
target.as_ptr() as *const _,
flags.bits(),
0,
shader.mut_void() as *mut *mut _,
error.mut_void() as *mut *mut _,
shader.mut_self(),
error.mut_self(),
)
};

Expand Down
2 changes: 1 addition & 1 deletion src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum Priority {
GlobalRealtime = d3d12::D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME,
}

bitflags! {
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CommandQueueFlags: u32 {
const DISABLE_GPU_TIMEOUT = d3d12::D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT;
Expand Down

0 comments on commit 81588ec

Please sign in to comment.