Skip to content

Commit

Permalink
use default rustfmt config for most crates
Browse files Browse the repository at this point in the history
  • Loading branch information
sivadeilra committed Jun 3, 2024
1 parent dbc3932 commit 6ebf9b1
Show file tree
Hide file tree
Showing 29 changed files with 2,198 additions and 428 deletions.
1 change: 1 addition & 0 deletions crates/libs/core/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
newline_style = "Unix"
9 changes: 8 additions & 1 deletion crates/libs/core/src/agile_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ impl<T: Interface> AgileReference<T> {
// For example, <T: Interface<UNKNOWN = true>>
// https://github.com/rust-lang/rust/issues/92827
assert!(T::UNKNOWN);
unsafe { imp::RoGetAgileReference(imp::AGILEREFERENCE_DEFAULT, &T::IID, core::mem::transmute::<&T, &IUnknown>(object)).map(|reference| Self(reference, Default::default())) }
unsafe {
imp::RoGetAgileReference(
imp::AGILEREFERENCE_DEFAULT,
&T::IID,
core::mem::transmute::<&T, &IUnknown>(object),
)
.map(|reference| Self(reference, Default::default()))
}
}

/// Retrieves a proxy to the target of the `AgileReference` object that may safely be used within any thread context in which get is called.
Expand Down
15 changes: 12 additions & 3 deletions crates/libs/core/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ pub struct Array<T: Type<T>> {

impl<T: Type<T>> Default for Array<T> {
fn default() -> Self {
Array { data: core::ptr::null_mut(), len: 0 }
Array {
data: core::ptr::null_mut(),
len: 0,
}
}
}

Expand All @@ -21,7 +24,9 @@ impl<T: Type<T>> Array<T> {
/// Creates an array of the given length with default values.
pub fn with_len(len: usize) -> Self {
assert!(len < u32::MAX as usize);
let bytes_amount = len.checked_mul(core::mem::size_of::<T>()).expect("Attempted to allocate too large an Array");
let bytes_amount = len
.checked_mul(core::mem::size_of::<T>())
.expect("Attempted to allocate too large an Array");

// WinRT arrays must be allocated with CoTaskMemAlloc.
// SAFETY: the call to CoTaskMemAlloc is safe to perform
Expand Down Expand Up @@ -159,7 +164,11 @@ pub struct ArrayProxy<T: Type<T>> {

impl<T: Type<T>> ArrayProxy<T> {
pub fn from_raw_parts(data: *mut *mut T::Default, len: *mut u32) -> Self {
Self { data, len, temp: core::mem::ManuallyDrop::new(Array::new()) }
Self {
data,
len,
temp: core::mem::ManuallyDrop::new(Array::new()),
}
}

pub fn as_array(&mut self) -> &mut Array<T> {
Expand Down
29 changes: 24 additions & 5 deletions crates/libs/core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ impl<T: Interface> Default for Event<T> {
impl<T: Interface> Event<T> {
/// Creates a new, empty `Event<T>`.
pub fn new() -> Self {
Self { delegates: Array::new(), swap: Mutex::default(), change: Mutex::default() }
Self {
delegates: Array::new(),
swap: Mutex::default(),
change: Mutex::default(),
}
}

/// Registers a delegate with the event object.
Expand Down Expand Up @@ -102,7 +106,10 @@ impl<T: Interface> Event<T> {
for delegate in lock_free_calls.as_slice() {
if let Err(error) = delegate.call(&mut callback) {
const RPC_E_SERVER_UNAVAILABLE: HRESULT = HRESULT(-2147023174); // HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE)
if matches!(error.code(), imp::RPC_E_DISCONNECTED | imp::JSCRIPT_E_CANTEXECUTE | RPC_E_SERVER_UNAVAILABLE) {
if matches!(
error.code(),
imp::RPC_E_DISCONNECTED | imp::JSCRIPT_E_CANTEXECUTE | RPC_E_SERVER_UNAVAILABLE
) {
self.remove(delegate.to_token())?;
}
}
Expand All @@ -127,12 +134,20 @@ impl<T: Interface> Default for Array<T> {
impl<T: Interface> Array<T> {
/// Creates a new, empty `Array<T>` with no capacity.
fn new() -> Self {
Self { buffer: null_mut(), len: 0, _phantom: PhantomData }
Self {
buffer: null_mut(),
len: 0,
_phantom: PhantomData,
}
}

/// Creates a new, empty `Array<T>` with the specified capacity.
fn with_capacity(capacity: usize) -> Result<Self> {
Ok(Self { buffer: Buffer::new(capacity)?, len: 0, _phantom: PhantomData })
Ok(Self {
buffer: Buffer::new(capacity)?,
len: 0,
_phantom: PhantomData,
})
}

/// Swaps the contents of two `Array<T>` objects.
Expand Down Expand Up @@ -184,7 +199,11 @@ impl<T: Interface> Clone for Array<T> {
if !self.is_empty() {
unsafe { (*self.buffer).0.add_ref() };
}
Self { buffer: self.buffer, len: self.len, _phantom: PhantomData }
Self {
buffer: self.buffer,
len: self.len,
_phantom: PhantomData,
}
}
}

Expand Down
70 changes: 60 additions & 10 deletions crates/libs/core/src/guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,48 @@ impl GUID {

/// Creates a `GUID` represented by the all-zero byte-pattern.
pub const fn zeroed() -> Self {
Self { data1: 0, data2: 0, data3: 0, data4: [0, 0, 0, 0, 0, 0, 0, 0] }
Self {
data1: 0,
data2: 0,
data3: 0,
data4: [0, 0, 0, 0, 0, 0, 0, 0],
}
}

/// Creates a `GUID` with the given constant values.
pub const fn from_values(data1: u32, data2: u16, data3: u16, data4: [u8; 8]) -> Self {
Self { data1, data2, data3, data4 }
Self {
data1,
data2,
data3,
data4,
}
}

/// Creates a `GUID` from a `u128` value.
pub const fn from_u128(uuid: u128) -> Self {
Self { data1: (uuid >> 96) as u32, data2: (uuid >> 80 & 0xffff) as u16, data3: (uuid >> 64 & 0xffff) as u16, data4: (uuid as u64).to_be_bytes() }
Self {
data1: (uuid >> 96) as u32,
data2: (uuid >> 80 & 0xffff) as u16,
data3: (uuid >> 64 & 0xffff) as u16,
data4: (uuid as u64).to_be_bytes(),
}
}

/// Converts a `GUID` to a `u128` value.
pub const fn to_u128(&self) -> u128 {
((self.data1 as u128) << 96) + ((self.data2 as u128) << 80) + ((self.data3 as u128) << 64) + u64::from_be_bytes(self.data4) as u128
((self.data1 as u128) << 96)
+ ((self.data2 as u128) << 80)
+ ((self.data3 as u128) << 64)
+ u64::from_be_bytes(self.data4) as u128
}

/// Creates a `GUID` for a "generic" WinRT type.
pub const fn from_signature(signature: imp::ConstBuffer) -> Self {
let data = imp::ConstBuffer::from_slice(&[0x11, 0xf4, 0x7a, 0xd5, 0x7b, 0x73, 0x42, 0xc0, 0xab, 0xae, 0x87, 0x8b, 0x1e, 0x16, 0xad, 0xee]);
let data = imp::ConstBuffer::from_slice(&[
0x11, 0xf4, 0x7a, 0xd5, 0x7b, 0x73, 0x42, 0xc0, 0xab, 0xae, 0x87, 0x8b, 0x1e, 0x16,
0xad, 0xee,
]);

let data = data.push_other(signature);

Expand All @@ -60,7 +81,14 @@ impl GUID {
third = (third & 0x0fff) | (5 << 12);
let fourth = (bytes[8] & 0x3f) | 0x80;

Self::from_values(first, second, third, [fourth, bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]])
Self::from_values(
first,
second,
third,
[
fourth, bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
],
)
}
}

Expand All @@ -74,7 +102,21 @@ impl TypeKind for GUID {

impl core::fmt::Debug for GUID {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:08X?}-{:04X?}-{:04X?}-{:02X?}{:02X?}-{:02X?}{:02X?}{:02X?}{:02X?}{:02X?}{:02X?}", self.data1, self.data2, self.data3, self.data4[0], self.data4[1], self.data4[2], self.data4[3], self.data4[4], self.data4[5], self.data4[6], self.data4[7])
write!(
f,
"{:08X?}-{:04X?}-{:04X?}-{:02X?}{:02X?}-{:02X?}{:02X?}{:02X?}{:02X?}{:02X?}{:02X?}",
self.data1,
self.data2,
self.data3,
self.data4[0],
self.data4[1],
self.data4[2],
self.data4[3],
self.data4[4],
self.data4[5],
self.data4[6],
self.data4[7]
)
}
}

Expand All @@ -83,11 +125,19 @@ impl From<&str> for GUID {
assert!(value.len() == 36, "Invalid GUID string");
let mut bytes = value.bytes();

let a = ((bytes.next_u32() * 16 + bytes.next_u32()) << 24) + ((bytes.next_u32() * 16 + bytes.next_u32()) << 16) + ((bytes.next_u32() * 16 + bytes.next_u32()) << 8) + bytes.next_u32() * 16 + bytes.next_u32();
let a = ((bytes.next_u32() * 16 + bytes.next_u32()) << 24)
+ ((bytes.next_u32() * 16 + bytes.next_u32()) << 16)
+ ((bytes.next_u32() * 16 + bytes.next_u32()) << 8)
+ bytes.next_u32() * 16
+ bytes.next_u32();
assert!(bytes.next().unwrap() == b'-', "Invalid GUID string");
let b = ((bytes.next_u16() * 16 + (bytes.next_u16())) << 8) + bytes.next_u16() * 16 + bytes.next_u16();
let b = ((bytes.next_u16() * 16 + (bytes.next_u16())) << 8)
+ bytes.next_u16() * 16
+ bytes.next_u16();
assert!(bytes.next().unwrap() == b'-', "Invalid GUID string");
let c = ((bytes.next_u16() * 16 + bytes.next_u16()) << 8) + bytes.next_u16() * 16 + bytes.next_u16();
let c = ((bytes.next_u16() * 16 + bytes.next_u16()) << 8)
+ bytes.next_u16() * 16
+ bytes.next_u16();
assert!(bytes.next().unwrap() == b'-', "Invalid GUID string");
let d = bytes.next_u8() * 16 + bytes.next_u8();
let e = bytes.next_u8() * 16 + bytes.next_u8();
Expand Down
18 changes: 15 additions & 3 deletions crates/libs/core/src/imp/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
#![allow(
non_snake_case,
non_upper_case_globals,
non_camel_case_types,
dead_code,
clippy::all
)]
windows_targets::link!("api-ms-win-core-winrt-l1-1-0.dll" "system" fn RoGetActivationFactory(activatableclassid : * mut core::ffi::c_void, iid : *const GUID, factory : *mut *mut core::ffi::c_void) -> HRESULT);
windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE);
Expand Down Expand Up @@ -325,7 +331,12 @@ pub struct GUID {
}
impl GUID {
pub const fn from_u128(uuid: u128) -> Self {
Self { data1: (uuid >> 96) as u32, data2: (uuid >> 80 & 0xffff) as u16, data3: (uuid >> 64 & 0xffff) as u16, data4: (uuid as u64).to_be_bytes() }
Self {
data1: (uuid >> 96) as u32,
data2: (uuid >> 80 & 0xffff) as u16,
data3: (uuid >> 64 & 0xffff) as u16,
data4: (uuid as u64).to_be_bytes(),
}
}
}
pub type HANDLE = isize;
Expand All @@ -343,7 +354,8 @@ pub type IMPLTYPEFLAGS = i32;
pub type INVOKEKIND = i32;
pub type LOAD_LIBRARY_FLAGS = u32;
pub const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: LOAD_LIBRARY_FLAGS = 4096u32;
pub type LPEXCEPFINO_DEFERRED_FILLIN = Option<unsafe extern "system" fn(pexcepinfo: *mut EXCEPINFO) -> HRESULT>;
pub type LPEXCEPFINO_DEFERRED_FILLIN =
Option<unsafe extern "system" fn(pexcepinfo: *mut EXCEPINFO) -> HRESULT>;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct PARAMDESC {
Expand Down
Loading

0 comments on commit 6ebf9b1

Please sign in to comment.