Skip to content

Commit

Permalink
Prefer explicit casts (#2582)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jul 21, 2023
1 parent 3cb54c7 commit f31b44e
Show file tree
Hide file tree
Showing 57 changed files with 339 additions and 312 deletions.
4 changes: 2 additions & 2 deletions crates/libs/core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ impl<T: ComInterface> Delegate<T> {
fn to_token(&self) -> i64 {
unsafe {
match self {
Self::Direct(delegate) => crate::imp::EncodePointer(std::mem::transmute_copy(delegate)) as _,
Self::Indirect(delegate) => crate::imp::EncodePointer(std::mem::transmute_copy(delegate)) as _,
Self::Direct(delegate) => crate::imp::EncodePointer(std::mem::transmute_copy(delegate)) as i64,
Self::Indirect(delegate) => crate::imp::EncodePointer(std::mem::transmute_copy(delegate)) as i64,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/core/src/hresult.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ impl HRESULT {
let mut message = HeapString(std::ptr::null_mut());

unsafe {
let size = crate::imp::FormatMessageW(crate::imp::FORMAT_MESSAGE_ALLOCATE_BUFFER | crate::imp::FORMAT_MESSAGE_FROM_SYSTEM | crate::imp::FORMAT_MESSAGE_IGNORE_INSERTS, std::ptr::null(), self.0 as _, 0, &mut message.0 as *mut _ as *mut _, 0, std::ptr::null());
let size = crate::imp::FormatMessageW(crate::imp::FORMAT_MESSAGE_ALLOCATE_BUFFER | crate::imp::FORMAT_MESSAGE_FROM_SYSTEM | crate::imp::FORMAT_MESSAGE_IGNORE_INSERTS, std::ptr::null(), self.0 as u32, 0, &mut message.0 as *mut _ as *mut _, 0, std::ptr::null());

HSTRING::from_wide(crate::imp::wide_trim_end(std::slice::from_raw_parts(message.0 as *const u16, size as usize))).unwrap_or_default()
}
}

/// Maps a Win32 error code to an HRESULT value.
pub(crate) fn from_win32(error: u32) -> Self {
Self(if error == 0 { 0 } else { (error & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as _)
Self(if error == 0 { 0 } else { (error & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as i32)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/imp/ref_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct RefCount(pub(crate) AtomicI32);
impl RefCount {
/// Creates a new `RefCount` with an initial value of `1`.
pub fn new(count: u32) -> Self {
Self(AtomicI32::new(count as _))
Self(AtomicI32::new(count as i32))
}

/// Increments the reference count, returning the new value.
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/core/src/imp/weak_ref_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ impl WeakRefCount {
return TearOff::from_encoding(count_or_pointer);
}

let tear_off = TearOff::new(object, count_or_pointer as _);
let tear_off = TearOff::new(object, count_or_pointer as u32);
let tear_off_ptr: *mut std::ffi::c_void = std::mem::transmute_copy(&tear_off);
let encoding: usize = ((tear_off_ptr as usize) >> 1) | (1 << (std::mem::size_of::<usize>() * 8 - 1));

loop {
match self.0.compare_exchange_weak(count_or_pointer, encoding as _, Ordering::AcqRel, Ordering::Relaxed) {
match self.0.compare_exchange_weak(count_or_pointer, encoding as isize, Ordering::AcqRel, Ordering::Relaxed) {
Ok(_) => {
let result: *mut std::ffi::c_void = std::mem::transmute(tear_off);
TearOff::from_strong_ptr(result).strong_count.add_ref();
Expand All @@ -61,7 +61,7 @@ impl WeakRefCount {
return TearOff::from_encoding(count_or_pointer);
}

TearOff::from_strong_ptr(tear_off_ptr).strong_count.0.store(count_or_pointer as _, Ordering::SeqCst);
TearOff::from_strong_ptr(tear_off_ptr).strong_count.0.store(count_or_pointer as i32, Ordering::SeqCst);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/strings/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl BSTR {
return Ok(Self::new());
}

let result = unsafe { Self(crate::imp::SysAllocStringLen(value.as_ptr(), value.len() as _)) };
let result = unsafe { Self(crate::imp::SysAllocStringLen(value.as_ptr(), value.len() as u32)) };

if result.is_empty() {
Err(crate::imp::E_OUTOFMEMORY.into())
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<'a> Blob<'a> {
let mut mods = vec![];
loop {
let (value, offset) = self.peek_usize();
if value != ELEMENT_TYPE_CMOD_OPT as _ && value != ELEMENT_TYPE_CMOD_REQD as _ {
if value != ELEMENT_TYPE_CMOD_OPT as usize && value != ELEMENT_TYPE_CMOD_REQD as usize {
break;
} else {
self.offset(offset);
Expand Down
16 changes: 8 additions & 8 deletions crates/libs/metadata/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum HasAttribute {

impl HasAttribute {
pub fn encode(&self) -> usize {
(match self {
match self {
Self::MethodDef(row) => (row.0.row + 1) << 5,
Self::Field(row) => ((row.0.row + 1) << 5) | 1,
Self::TypeRef(row) => ((row.0.row + 1) << 5) | 2,
Expand All @@ -42,7 +42,7 @@ impl HasAttribute {
Self::MemberRef(row) => ((row.0.row + 1) << 5) | 6,
Self::TypeSpec(row) => ((row.0.row + 1) << 5) | 13,
Self::GenericParam(row) => ((row.0.row + 1) << 5) | 19,
}) as _
}
}
}

Expand All @@ -53,9 +53,9 @@ pub enum HasConstant {

impl HasConstant {
pub fn encode(&self) -> usize {
(match self {
match self {
Self::Field(row) => (row.0.row + 1) << 2,
}) as _
}
}
}

Expand All @@ -66,9 +66,9 @@ pub enum MemberForwarded {

impl MemberForwarded {
pub fn encode(&self) -> usize {
(match self {
match self {
Self::MethodDef(value) => ((value.0.row + 1) << 1) | 1,
}) as _
}
}
}

Expand Down Expand Up @@ -111,9 +111,9 @@ pub enum TypeOrMethodDef {

impl TypeOrMethodDef {
pub fn encode(&self) -> usize {
(match self {
match self {
Self::TypeDef(value) => (value.0.row + 1) << 1,
}) as _
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/libs/metadata/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl File {

let dos = result.bytes.view_as::<IMAGE_DOS_HEADER>(0)?;

if dos.e_magic != IMAGE_DOS_SIGNATURE as _ || result.bytes.copy_as::<u32>(dos.e_lfanew as _)? != IMAGE_NT_SIGNATURE {
if dos.e_magic != IMAGE_DOS_SIGNATURE || result.bytes.copy_as::<u32>(dos.e_lfanew as usize)? != IMAGE_NT_SIGNATURE {
return Err(());
}

Expand All @@ -47,14 +47,14 @@ impl File {
_ => return Err(()),
};

let clr = result.bytes.view_as::<IMAGE_COR20_HEADER>(offset_from_rva(section_from_rva(sections, com_virtual_address)?, com_virtual_address) as _)?;
let clr = result.bytes.view_as::<IMAGE_COR20_HEADER>(offset_from_rva(section_from_rva(sections, com_virtual_address)?, com_virtual_address))?;

if clr.cb != std::mem::size_of::<IMAGE_COR20_HEADER>() as _ {
if clr.cb != std::mem::size_of::<IMAGE_COR20_HEADER>() as u32 {
return Err(());
}

let metadata_offset = offset_from_rva(section_from_rva(sections, clr.MetaData.VirtualAddress)?, clr.MetaData.VirtualAddress);
let metadata = result.bytes.view_as::<METADATA_HEADER>(metadata_offset as _)?;
let metadata = result.bytes.view_as::<METADATA_HEADER>(metadata_offset)?;

if metadata.signature != METADATA_SIGNATURE {
return Err(());
Expand Down Expand Up @@ -119,7 +119,7 @@ impl File {
continue;
}

let len = result.bytes.copy_as::<u32>(view)? as _;
let len = result.bytes.copy_as::<u32>(view)? as usize;
view += 4;

match i {
Expand Down
16 changes: 8 additions & 8 deletions crates/libs/metadata/src/file/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait RowReader<'a> {
fn row_usize<R: AsRow>(&self, row: R, column: usize) -> usize {
let file = self.row_file(row);
let row = row.to_row();
file.usize(row.row as _, R::TABLE as _, column)
file.usize(row.row, R::TABLE, column)
}

fn row_str<R: AsRow>(&self, row: R, column: usize) -> &'a str {
Expand Down Expand Up @@ -148,7 +148,7 @@ pub trait RowReader<'a> {
//

fn field_flags(&self, row: Field) -> FieldAttributes {
FieldAttributes(self.row_usize(row, 0) as _)
FieldAttributes(self.row_usize(row, 0) as u16)
}

fn field_name(&self, row: Field) -> &'a str {
Expand Down Expand Up @@ -216,7 +216,7 @@ pub trait RowReader<'a> {
}

fn method_def_flags(&self, row: MethodDef) -> MethodAttributes {
MethodAttributes(self.row_usize(row, 2) as _)
MethodAttributes(self.row_usize(row, 2) as u16)
}

fn method_def_name(&self, row: MethodDef) -> &'a str {
Expand Down Expand Up @@ -269,7 +269,7 @@ pub trait RowReader<'a> {
//

fn param_flags(&self, row: Param) -> ParamAttributes {
ParamAttributes(self.row_usize(row, 0) as _)
ParamAttributes(self.row_usize(row, 0) as u16)
}

fn param_sequence(&self, row: Param) -> usize {
Expand All @@ -289,7 +289,7 @@ pub trait RowReader<'a> {
//

fn type_def_flags(&self, row: TypeDef) -> TypeAttributes {
TypeAttributes(self.row_usize(row, 0) as _)
TypeAttributes(self.row_usize(row, 0) as u32)
}

fn type_def_name(&self, row: TypeDef) -> &'a str {
Expand Down Expand Up @@ -324,15 +324,15 @@ pub trait RowReader<'a> {
}

fn type_def_interface_impls(&self, row: TypeDef) -> RowIterator<InterfaceImpl> {
self.row_equal_range(row, 0, (row.0.row + 1) as _)
self.row_equal_range(row, 0, row.0.row + 1)
}

fn type_def_enclosing_type(&self, row: TypeDef) -> Option<TypeDef> {
self.row_equal_range::<TypeDef, NestedClass>(row, 0, (row.0.row + 1) as _).next().map(|row| TypeDef(Row::new(self.row_usize(row, 1) - 1, row.file())))
self.row_equal_range::<TypeDef, NestedClass>(row, 0, row.0.row + 1).next().map(|row| TypeDef(Row::new(self.row_usize(row, 1) - 1, row.file())))
}

fn type_def_class_layout(&self, row: TypeDef) -> Option<ClassLayout> {
self.row_equal_range(row, 2, (row.0.row + 1) as _).next()
self.row_equal_range(row, 2, row.0.row + 1).next()
}

fn type_def_is_scoped(&self, row: TypeDef) -> bool {
Expand Down
20 changes: 10 additions & 10 deletions crates/libs/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl<'a> Reader<'a> {
let _id = values.read_u8();
let arg_type = values.read_u8();
let mut name = values.read_str().to_string();
let arg = match arg_type as _ {
let arg = match arg_type {
ELEMENT_TYPE_BOOLEAN => Value::Bool(values.read_bool()),
ELEMENT_TYPE_I2 => Value::I16(values.read_i16()),
ELEMENT_TYPE_I4 => Value::I32(values.read_i32()),
Expand Down Expand Up @@ -447,7 +447,7 @@ impl<'a> Reader<'a> {
}
pub fn method_def_signature(&self, namespace: &str, row: MethodDef, generics: &[Type]) -> Signature {
let mut blob = self.row_blob(row, 4);
let call_flags = MethodCallAttributes(blob.read_usize() as _);
let call_flags = MethodCallAttributes(blob.read_usize() as u8);
let _param_count = blob.read_usize();
let mut return_type = self.type_from_blob(&mut blob, None, generics);

Expand Down Expand Up @@ -635,16 +635,16 @@ impl<'a> Reader<'a> {
"NativeArrayInfoAttribute" => {
for (_, value) in self.attribute_args(attribute) {
match value {
Value::I16(value) => return SignatureParamKind::ArrayRelativeLen(value as _),
Value::I32(value) => return SignatureParamKind::ArrayFixed(value as _),
Value::I16(value) => return SignatureParamKind::ArrayRelativeLen(value as usize),
Value::I32(value) => return SignatureParamKind::ArrayFixed(value as usize),
_ => {}
}
}
}
"MemorySizeAttribute" => {
for (_, value) in self.attribute_args(attribute) {
if let Value::I16(value) = value {
return SignatureParamKind::ArrayRelativeByteLen(value as _);
return SignatureParamKind::ArrayRelativeByteLen(value as usize);
}
}
}
Expand Down Expand Up @@ -1527,17 +1527,17 @@ impl<'a> Reader<'a> {

// Used by WinRT to indicate an output parameter, but there are other ways to determine this direction so here
// it is only used to distinguish between slices and heap-allocated arrays.
let is_ref = blob.read_expected(ELEMENT_TYPE_BYREF as _);
let is_ref = blob.read_expected(ELEMENT_TYPE_BYREF as usize);

if blob.read_expected(ELEMENT_TYPE_VOID as _) {
if blob.read_expected(ELEMENT_TYPE_VOID as usize) {
return Type::Void;
}

let is_array = blob.read_expected(ELEMENT_TYPE_SZARRAY as _); // Used by WinRT to indicate an array
let is_array = blob.read_expected(ELEMENT_TYPE_SZARRAY as usize); // Used by WinRT to indicate an array

let mut pointers = 0;

while blob.read_expected(ELEMENT_TYPE_PTR as _) {
while blob.read_expected(ELEMENT_TYPE_PTR as usize) {
pointers += 1;
}

Expand All @@ -1564,7 +1564,7 @@ impl<'a> Reader<'a> {
return code;
}

match code as _ {
match code as u8 {
ELEMENT_TYPE_VALUETYPE | ELEMENT_TYPE_CLASS => self.type_from_ref(TypeDefOrRef::decode(blob.file, blob.read_usize()), enclosing, generics),
ELEMENT_TYPE_VAR => generics.get(blob.read_usize()).unwrap_or(&Type::Void).clone(),
ELEMENT_TYPE_ARRAY => {
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/metadata/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Row {

impl Row {
pub fn new(row: usize, file: usize) -> Self {
Self { row: row as _, file: file as _ }
Self { row, file }
}

fn next(&self) -> Self {
Expand Down Expand Up @@ -48,7 +48,7 @@ impl<R: AsRow> Iterator for RowIterator<R> {
type Item = R;

fn next(&mut self) -> Option<Self::Item> {
self.rows.next().map(|row| R::from_row(Row::new(row, self.file as _)))
self.rows.next().map(|row| R::from_row(Row::new(row, self.file)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/metadata/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Type {
/// Creates a `Type` object from an `ELEMENT_TYPE` (see ECMA-335) type constant, typically
/// used to indicate the type of a constant or primitive type signature.
pub fn from_code(code: usize) -> Option<Self> {
match code as _ {
match code as u8 {
ELEMENT_TYPE_VOID => Some(Self::Void),
ELEMENT_TYPE_BOOLEAN => Some(Self::Bool),
ELEMENT_TYPE_CHAR => Some(Self::Char),
Expand Down
12 changes: 6 additions & 6 deletions crates/libs/windows/src/Windows/Devices/I2c/Provider/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ impl II2cDeviceProvider_Vtbl {
unsafe extern "system" fn Write<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: II2cDeviceProvider_Impl, const OFFSET: isize>(this: *mut ::core::ffi::c_void, buffer_array_size: u32, buffer: *const u8) -> ::windows_core::HRESULT {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
this.Write(::core::slice::from_raw_parts(::core::mem::transmute_copy(&buffer), buffer_array_size as _)).into()
this.Write(::core::slice::from_raw_parts(::core::mem::transmute_copy(&buffer), buffer_array_size as usize)).into()
}
unsafe extern "system" fn WritePartial<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: II2cDeviceProvider_Impl, const OFFSET: isize>(this: *mut ::core::ffi::c_void, buffer_array_size: u32, buffer: *const u8, result__: *mut ProviderI2cTransferResult) -> ::windows_core::HRESULT {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
match this.WritePartial(::core::slice::from_raw_parts(::core::mem::transmute_copy(&buffer), buffer_array_size as _)) {
match this.WritePartial(::core::slice::from_raw_parts(::core::mem::transmute_copy(&buffer), buffer_array_size as usize)) {
::core::result::Result::Ok(ok__) => {
::core::ptr::write(result__, ::core::mem::transmute_copy(&ok__));
::windows_core::HRESULT(0)
Expand All @@ -79,12 +79,12 @@ impl II2cDeviceProvider_Vtbl {
unsafe extern "system" fn Read<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: II2cDeviceProvider_Impl, const OFFSET: isize>(this: *mut ::core::ffi::c_void, buffer_array_size: u32, buffer: *mut u8) -> ::windows_core::HRESULT {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
this.Read(::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&buffer), buffer_array_size as _)).into()
this.Read(::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&buffer), buffer_array_size as usize)).into()
}
unsafe extern "system" fn ReadPartial<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: II2cDeviceProvider_Impl, const OFFSET: isize>(this: *mut ::core::ffi::c_void, buffer_array_size: u32, buffer: *mut u8, result__: *mut ProviderI2cTransferResult) -> ::windows_core::HRESULT {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
match this.ReadPartial(::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&buffer), buffer_array_size as _)) {
match this.ReadPartial(::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&buffer), buffer_array_size as usize)) {
::core::result::Result::Ok(ok__) => {
::core::ptr::write(result__, ::core::mem::transmute_copy(&ok__));
::windows_core::HRESULT(0)
Expand All @@ -95,12 +95,12 @@ impl II2cDeviceProvider_Vtbl {
unsafe extern "system" fn WriteRead<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: II2cDeviceProvider_Impl, const OFFSET: isize>(this: *mut ::core::ffi::c_void, writeBuffer_array_size: u32, writebuffer: *const u8, readBuffer_array_size: u32, readbuffer: *mut u8) -> ::windows_core::HRESULT {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
this.WriteRead(::core::slice::from_raw_parts(::core::mem::transmute_copy(&writebuffer), writeBuffer_array_size as _), ::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&readbuffer), readBuffer_array_size as _)).into()
this.WriteRead(::core::slice::from_raw_parts(::core::mem::transmute_copy(&writebuffer), writeBuffer_array_size as usize), ::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&readbuffer), readBuffer_array_size as usize)).into()
}
unsafe extern "system" fn WriteReadPartial<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: II2cDeviceProvider_Impl, const OFFSET: isize>(this: *mut ::core::ffi::c_void, writeBuffer_array_size: u32, writebuffer: *const u8, readBuffer_array_size: u32, readbuffer: *mut u8, result__: *mut ProviderI2cTransferResult) -> ::windows_core::HRESULT {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
match this.WriteReadPartial(::core::slice::from_raw_parts(::core::mem::transmute_copy(&writebuffer), writeBuffer_array_size as _), ::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&readbuffer), readBuffer_array_size as _)) {
match this.WriteReadPartial(::core::slice::from_raw_parts(::core::mem::transmute_copy(&writebuffer), writeBuffer_array_size as usize), ::core::slice::from_raw_parts_mut(::core::mem::transmute_copy(&readbuffer), readBuffer_array_size as usize)) {
::core::result::Result::Ok(ok__) => {
::core::ptr::write(result__, ::core::mem::transmute_copy(&ok__));
::windows_core::HRESULT(0)
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/windows/src/Windows/Devices/Sms/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ISmsBinaryMessage_Vtbl {
unsafe extern "system" fn SetData<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: ISmsBinaryMessage_Impl, const OFFSET: isize>(this: *mut ::core::ffi::c_void, value_array_size: u32, value: *const u8) -> ::windows_core::HRESULT {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
this.SetData(::core::slice::from_raw_parts(::core::mem::transmute_copy(&value), value_array_size as _)).into()
this.SetData(::core::slice::from_raw_parts(::core::mem::transmute_copy(&value), value_array_size as usize)).into()
}
Self {
base__: ::windows_core::IInspectable_Vtbl::new::<Identity, ISmsBinaryMessage, OFFSET>(),
Expand Down
Loading

0 comments on commit f31b44e

Please sign in to comment.