Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to support Edition 2024 of Rust in future #3530

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/types/cpp_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl CppConst {
}
} else if let Some(attribute) = self.field.find_attribute("ConstantAttribute") {
let args = attribute.args();
let Some((_, Value::Str(mut input))) = args.first() else {
let Some(&(_, Value::Str(mut input))) = args.first() else {
panic!()
};

Expand Down
5 changes: 3 additions & 2 deletions crates/libs/implement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use quote::{quote, ToTokens};

mod gen;
mod r#gen;
use r#gen::gen_all;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -75,7 +76,7 @@ fn implement_core(
original_type,
};

let items = gen::gen_all(&inputs);
let items = gen_all(&inputs);
let mut tokens = inputs.original_type.into_token_stream();
for item in items {
tokens.extend(item.into_token_stream());
Expand Down
4 changes: 1 addition & 3 deletions crates/libs/strings/src/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ macro_rules! h {
($s:literal) => {{
const INPUT: &[u8] = $s.as_bytes();
const OUTPUT_LEN: usize = $crate::utf16_len(INPUT) + 1;
#[allow(clippy::declare_interior_mutable_const)]
const RESULT: $crate::HSTRING = {
static RESULT: $crate::HSTRING = {
if OUTPUT_LEN == 1 {
unsafe { ::core::mem::transmute(::core::ptr::null::<u16>()) }
} else {
Expand All @@ -60,7 +59,6 @@ macro_rules! h {
}
}
};
#[allow(clippy::borrow_interior_mutable_const)]
&RESULT
}};
}
Expand Down
114 changes: 62 additions & 52 deletions crates/samples/components/json_validator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ unsafe extern "system" fn CreateJsonValidator(
schema_len: usize,
handle: *mut usize,
) -> HRESULT {
create_validator(schema, schema_len, handle).into()
unsafe { create_validator(schema, schema_len, handle).into() }
}

// Validates a JSON value against a previously-compiled schema.
Expand All @@ -21,39 +21,45 @@ unsafe extern "system" fn ValidateJson(
sanitized_value: *mut *mut u8,
sanitized_value_len: *mut usize,
) -> HRESULT {
validate(
handle,
value,
value_len,
sanitized_value,
sanitized_value_len,
)
.into()
unsafe {
validate(
handle,
value,
value_len,
sanitized_value,
sanitized_value_len,
)
.into()
}
}

// Closes a JSON validator object.
#[no_mangle]
unsafe extern "system" fn CloseJsonValidator(handle: usize) {
if handle != 0 {
_ = Box::from_raw(handle as *mut Validator);
unsafe {
if handle != 0 {
_ = Box::from_raw(handle as *mut Validator);
}
}
}

// Implementation of the `CreateJsonValidator` function so we can use `Result` for simplicity.
unsafe fn create_validator(schema: *const u8, schema_len: usize, handle: *mut usize) -> Result<()> {
let schema = json_from_raw_parts(schema, schema_len)?;
unsafe {
let schema = json_from_raw_parts(schema, schema_len)?;

let compiled =
Validator::new(&schema).map_err(|error| Error::new(E_INVALIDARG, error.to_string()))?;
let compiled =
Validator::new(&schema).map_err(|error| Error::new(E_INVALIDARG, error.to_string()))?;

if handle.is_null() {
return Err(E_POINTER.into());
}
if handle.is_null() {
return Err(E_POINTER.into());
}

// The handle is not null so we can safely dereference it here.
*handle = Box::into_raw(Box::new(compiled)) as usize;
// The handle is not null so we can safely dereference it here.
*handle = Box::into_raw(Box::new(compiled)) as usize;

Ok(())
Ok(())
}
}

// Implementation of the `ValidateJson` function so we can use `Result` for simplicity.
Expand All @@ -64,56 +70,60 @@ unsafe fn validate(
sanitized_value: *mut *mut u8,
sanitized_value_len: *mut usize,
) -> Result<()> {
if handle == 0 {
return Err(E_HANDLE.into());
}
unsafe {
if handle == 0 {
return Err(E_HANDLE.into());
}

let value = json_from_raw_parts(value, value_len)?;

let value = json_from_raw_parts(value, value_len)?;
// This looks a bit tricky but we're just turning the opaque handle into `Validator` pointer
// and then returning a reference to avoid taking ownership of it.
let schema = &*(handle as *const Validator);

// This looks a bit tricky but we're just turning the opaque handle into `Validator` pointer
// and then returning a reference to avoid taking ownership of it.
let schema = &*(handle as *const Validator);
if schema.is_valid(&value) {
if !sanitized_value.is_null() && !sanitized_value_len.is_null() {
let value = value.to_string();

if schema.is_valid(&value) {
if !sanitized_value.is_null() && !sanitized_value_len.is_null() {
let value = value.to_string();
*sanitized_value = CoTaskMemAlloc(value.len()) as _;

*sanitized_value = CoTaskMemAlloc(value.len()) as _;
if (*sanitized_value).is_null() {
return Err(E_OUTOFMEMORY.into());
}

if (*sanitized_value).is_null() {
return Err(E_OUTOFMEMORY.into());
(*sanitized_value).copy_from(value.as_ptr(), value.len());
*sanitized_value_len = value.len();
}

(*sanitized_value).copy_from(value.as_ptr(), value.len());
*sanitized_value_len = value.len();
}
Ok(())
} else {
let mut message = String::new();

Ok(())
} else {
let mut message = String::new();
// The `validate` method returns a collection of errors. We'll just return the first
// for simplicity.
if let Some(error) = schema.validate(&value).unwrap_err().next() {
message = error.to_string();
}

// The `validate` method returns a collection of errors. We'll just return the first
// for simplicity.
if let Some(error) = schema.validate(&value).unwrap_err().next() {
message = error.to_string();
Err(Error::new(E_INVALIDARG, message))
}

Err(Error::new(E_INVALIDARG, message))
}
}

// Takes care of all the JSON parsing and parameter validation.
unsafe fn json_from_raw_parts(value: *const u8, value_len: usize) -> Result<serde_json::Value> {
if value.is_null() {
return Err(E_POINTER.into());
}
unsafe {
if value.is_null() {
return Err(E_POINTER.into());
}

let value = std::slice::from_raw_parts(value, value_len);
let value = std::slice::from_raw_parts(value, value_len);

let value =
std::str::from_utf8(value).map_err(|_| Error::from(ERROR_NO_UNICODE_TRANSLATION))?;
let value =
std::str::from_utf8(value).map_err(|_| Error::from(ERROR_NO_UNICODE_TRANSLATION))?;

serde_json::from_str(value).map_err(|error| Error::new(E_INVALIDARG, format!("{error}")))
serde_json::from_str(value).map_err(|error| Error::new(E_INVALIDARG, format!("{error}")))
}
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#[test]
fn test() {
extern "system" {
unsafe extern "system" {
fn client();
}
unsafe {
Expand Down
30 changes: 16 additions & 14 deletions crates/samples/windows-sys/delay_load/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ use windows_sys::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::*
///
/// The `PCSTR` parameters need to be valid for reads up until and including the next `\0`.
pub unsafe fn delay_load<T>(library: PCSTR, function: PCSTR) -> Option<T> {
let library = LoadLibraryExA(
library,
core::ptr::null_mut(),
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
);
unsafe {
let library = LoadLibraryExA(
library,
core::ptr::null_mut(),
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
);

if library.is_null() {
return None;
}
if library.is_null() {
return None;
}

let address = GetProcAddress(library, function);
let address = GetProcAddress(library, function);

if address.is_some() {
return Some(std::mem::transmute_copy(&address));
}
if address.is_some() {
return Some(std::mem::transmute_copy(&address));
}

FreeLibrary(library);
None
FreeLibrary(library);
None
}
}

fn main() {
Expand Down
13 changes: 8 additions & 5 deletions crates/samples/windows/data_protection/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ fn main() -> std::io::Result<()> {

unsafe fn as_mut_bytes(buffer: &IBuffer) -> Result<&mut [u8]> {
let interop = buffer.cast::<IBufferByteAccess>()?;
let data = interop.Buffer()?;
Ok(std::slice::from_raw_parts_mut(
data,
buffer.Length()? as usize,
))

unsafe {
let data = interop.Buffer()?;
Ok(std::slice::from_raw_parts_mut(
data,
buffer.Length()? as usize,
))
}
}
22 changes: 12 additions & 10 deletions crates/samples/windows/delay_load/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ use windows::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::*};
///
/// The `PCSTR` parameters need to be valid for reads up until and including the next `\0`.
pub unsafe fn delay_load<T>(library: PCSTR, function: PCSTR) -> Option<T> {
let library = LoadLibraryExA(library, None, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
unsafe {
let library = LoadLibraryExA(library, None, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);

let Ok(library) = library else {
return None;
};
let Ok(library) = library else {
return None;
};

let address = GetProcAddress(library, function);
let address = GetProcAddress(library, function);

if address.is_some() {
return Some(std::mem::transmute_copy(&address));
}
if address.is_some() {
return Some(std::mem::transmute_copy(&address));
}

_ = FreeLibrary(library);
None
_ = FreeLibrary(library);
None
}
}

fn main() {
Expand Down
6 changes: 4 additions & 2 deletions crates/samples/windows/memory_buffer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ unsafe fn as_mut_slice(buffer: &IMemoryBufferReference) -> Result<&mut [u8]> {
let mut data = std::ptr::null_mut();
let mut len = 0;

interop.GetBuffer(&mut data, &mut len)?;
Ok(std::slice::from_raw_parts_mut(data, len as usize))
unsafe {
interop.GetBuffer(&mut data, &mut len)?;
Ok(std::slice::from_raw_parts_mut(data, len as usize))
}
}

fn main() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/libs/core/tests/unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Test {

impl ITest_Impl for Test_Impl {
unsafe fn Test(&self) -> u32 {
*self.drop
unsafe { *self.drop }
}
}

Expand Down
18 changes: 13 additions & 5 deletions crates/tests/libs/interface/tests/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ struct PersistState {

impl ICustomPersist_Impl for Persist_Impl {
unsafe fn GetClassID(&self, clsid: *mut GUID) -> HRESULT {
*clsid = "117fb826-2155-483a-b50d-bc99a2c7cca3".try_into().unwrap();
S_OK
unsafe {
*clsid = "117fb826-2155-483a-b50d-bc99a2c7cca3".try_into().unwrap();
S_OK
}
}
}

Expand All @@ -79,7 +81,9 @@ impl ICustomPersistMemory_Impl for Persist_Impl {
unsafe fn Load(&self, input: *const core::ffi::c_void, size: u32) -> HRESULT {
let mut writer = self.0.write().unwrap();
if size <= writer.memory.len() as u32 {
std::ptr::copy(input, writer.memory.as_mut_ptr() as _, size as usize);
unsafe {
std::ptr::copy(input, writer.memory.as_mut_ptr() as _, size as usize);
}
writer.dirty = true;
S_OK
} else {
Expand All @@ -90,7 +94,9 @@ impl ICustomPersistMemory_Impl for Persist_Impl {
unsafe fn Save(&self, output: *mut core::ffi::c_void, clear_dirty: BOOL, size: u32) -> HRESULT {
let mut writer = self.0.write().unwrap();
if size <= writer.memory.len() as u32 {
std::ptr::copy(writer.memory.as_mut_ptr() as _, output, size as usize);
unsafe {
std::ptr::copy(writer.memory.as_mut_ptr() as _, output, size as usize);
}
if clear_dirty.as_bool() {
writer.dirty = false;
}
Expand All @@ -102,7 +108,9 @@ impl ICustomPersistMemory_Impl for Persist_Impl {

unsafe fn GetSizeMax(&self, len: *mut u32) -> HRESULT {
let reader = self.0.read().unwrap();
*len = reader.memory.len() as u32;
unsafe {
*len = reader.memory.len() as u32;
}
S_OK
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tests/libs/interface/tests/non_com_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl IBase_Impl for Base {
}

unsafe fn base_value(test: &IBase) -> i32 {
test.BaseValue()
unsafe { test.BaseValue() }
}

#[test]
Expand Down
Loading