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

The scope of the unsafe block can be appropriately reduced #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions native-windows-gui/examples/sync-draw/src/shared_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl SharedMemory {
/// Fetch the next instance id in the shared memory.
pub fn next_instance_id(&self) -> u32 {
let header_ptr = SharedMemory::map_view(self.handle, 0, HEADER_SIZE) as *mut SharedHeader;
let next_id = unsafe {
let header = &mut *header_ptr;
let next_id = {
let header = unsafe { &mut *header_ptr };
header.next_instance_id += 1;
header.next_instance_id
};
Expand Down Expand Up @@ -234,14 +234,14 @@ impl SharedMemory {

/// Safe wrapper over MapViewOfFile
fn map_view(handle: HANDLE, offset: DWORD, size: SIZE_T) -> *mut u8 {
unsafe {
let handle = MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, offset, size) as *mut u8;

let handle = unsafe { MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, offset, size) as *mut u8 };
if handle.is_null() {
panic!("Could not map memory region");
}

handle
}

}

/// Safe wrapper over UnmapViewOfFile
Expand Down
28 changes: 14 additions & 14 deletions native-windows-gui/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ impl ToolTipTextData {
}

self.clear();
let data = unsafe { &mut *self.data };
let local_text = to_utf16(text);
unsafe {
let data = &mut *self.data;
let local_text = to_utf16(text);
ptr::copy_nonoverlapping(local_text.as_ptr(), data.szText.as_mut_ptr(), text_len);
}
}
Expand Down Expand Up @@ -539,11 +539,11 @@ impl PaintData {

/// Wrapper over BeginPaint
pub fn begin_paint(&self) -> PAINTSTRUCT {
unsafe {
let mut paint: PAINTSTRUCT = ::std::mem::zeroed();
BeginPaint(self.hwnd, &mut paint);

let mut paint: PAINTSTRUCT = unsafe { ::std::mem::zeroed() };
unsafe { BeginPaint(self.hwnd, &mut paint) };
paint
}

}

/// Wrapper over EndPaint
Expand All @@ -568,11 +568,11 @@ impl DropFiles {
pub fn point(&self) -> [i32; 2] {
use winapi::um::shellapi::DragQueryPoint;

unsafe {

let mut pt = POINT { x: 0, y: 0 };
DragQueryPoint(self.drop, &mut pt);
unsafe { DragQueryPoint(self.drop, &mut pt) };
[pt.x, pt.y]
}

}

/// Return the number of files dropped
Expand All @@ -593,19 +593,19 @@ impl DropFiles {

let len = self.len();
let mut files = Vec::with_capacity(len);
unsafe {

for i in 0..len {
// Need to add a +1 here for some reason
let buffer_size = (DragQueryFileW(self.drop, i as _, ptr::null_mut(), 0) + 1) as usize;
let buffer_size = unsafe { (DragQueryFileW(self.drop, i as _, ptr::null_mut(), 0) + 1) as usize };

let mut buffer: Vec<u16> = Vec::with_capacity(buffer_size);
buffer.set_len(buffer_size);
unsafe { buffer.set_len(buffer_size) };

DragQueryFileW(self.drop, i as _, buffer.as_mut_ptr(), buffer_size as _);
unsafe { DragQueryFileW(self.drop, i as _, buffer.as_mut_ptr(), buffer_size as _) };

files.push(from_utf16(&buffer));
}
}


files
}
Expand Down
10 changes: 5 additions & 5 deletions native-windows-gui/src/win32/window_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ pub fn destroy_window(hwnd: HWND) {
pub fn destroy_menu_item(parent: HMENU, item_id: u32) {
use winapi::um::winuser::{DeleteMenu, GetMenuItemCount, GetMenuItemID, MF_BYPOSITION};

unsafe {
let count = GetMenuItemCount(parent);

let count = unsafe { GetMenuItemCount(parent) };
let mut index = 0;

while index < count {
let id = GetMenuItemID(parent, index);
let id = unsafe { GetMenuItemID(parent, index) };
match id == item_id {
true => {
DeleteMenu(parent, index as u32, MF_BYPOSITION);
unsafe { DeleteMenu(parent, index as u32, MF_BYPOSITION) };
index = count;
},
false => {
index += 1;
}
}
}
}

}

pub fn destroy_menu(menu: HMENU) {
Expand Down