Skip to content

Commit d20fc4e

Browse files
committed
Take generic value type
1 parent 0475e2f commit d20fc4e

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

Diff for: library/std/src/os/windows/process.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
#![stable(feature = "process_extensions", since = "1.2.0")]
66

7-
use crate::ffi::{c_void, OsStr};
7+
use crate::ffi::OsStr;
88
use crate::os::windows::io::{
99
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
1010
};
@@ -166,17 +166,14 @@ pub trait CommandExt: Sealed {
166166
///
167167
/// # Safety
168168
///
169-
/// - The data pointed to by `value` pointer must not be moved or aliased for the entire lifetime of
170-
/// the `Command`.
171-
/// - The data pointed to by `value` pointer must be initialized.
172-
/// - `size` must not exceed the size of the object pointed to by the `value` pointer.
173-
/// - It must be safe to read the data pointed to by `value` from another thread.
169+
/// - The attribute and value pair must be supplied in accordance with [Win32 API usage][1].
170+
///
171+
/// [1]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-updateprocthreadattribute
174172
#[unstable(feature = "windows_process_extensions_proc_thread_attributes", issue = "none")]
175-
unsafe fn process_thread_attribute(
173+
unsafe fn process_thread_attribute<T: Copy + Send + Sync + 'static>(
176174
&mut self,
177175
attribute: usize,
178-
value: *mut c_void,
179-
size: usize,
176+
value: T,
180177
) -> &mut process::Command;
181178
}
182179

@@ -196,13 +193,12 @@ impl CommandExt for process::Command {
196193
self.as_inner_mut().raw_arg(raw_text.as_ref());
197194
self
198195
}
199-
unsafe fn process_thread_attribute(
196+
unsafe fn process_thread_attribute<T: Copy + Send + Sync + 'static>(
200197
&mut self,
201198
attribute: usize,
202-
value: *mut c_void,
203-
size: usize,
199+
value: T,
204200
) -> &mut process::Command {
205-
self.as_inner_mut().process_thread_attribute(attribute, value, size);
201+
self.as_inner_mut().process_thread_attribute(attribute, value);
206202
self
207203
}
208204
}

Diff for: library/std/src/process/tests.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -412,17 +412,15 @@ fn test_creation_flags() {
412412
fn test_proc_thread_attributes() {
413413
use crate::os::windows::io::AsRawHandle;
414414
use crate::os::windows::process::CommandExt;
415-
use crate::sys::c::{DWORD_PTR, HANDLE};
415+
use crate::sys::c::DWORD_PTR;
416416
const PROC_THREAD_ATTRIBUTE_PARENT_PROCESS: DWORD_PTR = 0x00020000;
417-
let mut parent = Command::new("cmd.exe").spawn().unwrap();
418-
let mut parent_handle: HANDLE = parent.as_raw_handle();
419417

418+
let mut parent = Command::new("cmd.exe").spawn().unwrap();
420419
let mut child_cmd = Command::new("cmd.exe");
421420
unsafe {
422421
child_cmd.process_thread_attribute(
423422
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
424-
&mut parent_handle as *mut _ as *mut _,
425-
crate::mem::size_of::<HANDLE>(),
423+
parent.as_raw_handle() as isize,
426424
);
427425
}
428426
let mut child = child_cmd.spawn().unwrap();

Diff for: library/std/src/sys/windows/process.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,15 @@ impl Command {
254254
pub fn get_current_dir(&self) -> Option<&Path> {
255255
self.cwd.as_ref().map(|cwd| Path::new(cwd))
256256
}
257-
pub fn process_thread_attribute(&mut self, attribute: usize, ptr: *mut c_void, size: usize) {
258-
self.proc_thread_attributes.insert(attribute, ProcThreadAttributeValue { ptr, size });
257+
pub unsafe fn process_thread_attribute<T: Copy + Send + Sync + 'static>(
258+
&mut self,
259+
attribute: usize,
260+
value: T,
261+
) {
262+
self.proc_thread_attributes.insert(
263+
attribute,
264+
ProcThreadAttributeValue { size: mem::size_of::<T>(), data: Box::new(value) },
265+
);
259266
}
260267

261268
pub fn spawn(
@@ -876,16 +883,10 @@ impl Drop for ProcThreadAttributeList {
876883
}
877884
}
878885
/// Wrapper around the value data to be used as a Process Thread Attribute.
879-
/// This exists primarily to force the raw pointer type to be `Send` and `Sync`
880-
/// without needing to `unsafe impl` them for `Command`.
881-
#[derive(Copy, Clone)]
882886
struct ProcThreadAttributeValue {
883-
ptr: *mut c_void,
887+
data: Box<dyn Send + Sync>,
884888
size: usize,
885889
}
886-
unsafe impl Send for ProcThreadAttributeValue {}
887-
unsafe impl Sync for ProcThreadAttributeValue {}
888-
889890
fn make_proc_thread_attributes(
890891
attributes: &BTreeMap<usize, ProcThreadAttributeValue>,
891892
) -> io::Result<ProcThreadAttributeList> {
@@ -902,13 +903,17 @@ fn make_proc_thread_attributes(
902903
// Add our attributes to the buffer.
903904
// It's theoretically possible for the count to overflow a u32. Therefore, make
904905
// sure we don't add more attributes than we actually initialized the buffer for.
905-
for (&attribute, &value) in attributes.iter().take(count as usize) {
906+
for (&attribute, value) in attributes.iter().take(count as usize) {
907+
let value_ptr: *const (dyn Send + Sync) = &*value.data as *const _;
908+
// let value_ptr = value_ptr as *const _;
909+
let value_ptr = value_ptr as *const c_void;
910+
let value_ptr = value_ptr as *mut c_void;
906911
cvt(unsafe {
907912
c::UpdateProcThreadAttribute(
908913
attribute_list.0.as_mut_ptr().cast(),
909914
0,
910915
attribute,
911-
value.ptr,
916+
value_ptr,
912917
value.size,
913918
ptr::null_mut(),
914919
ptr::null_mut(),

0 commit comments

Comments
 (0)