Add # Safety docs to wgpu-remote

Only two unsafe functions were used internally:

- `slice::from_raw_parts`
- `Box::from_raw`

The safety messages are adapted from the safety messages of these functions.
This commit is contained in:
yanchith
2020-01-17 15:04:57 +01:00
parent 0d9ad70468
commit 8414b01403
2 changed files with 44 additions and 0 deletions

View File

@@ -70,12 +70,21 @@ pub extern "C" fn wgpu_client_new() -> Infrastructure {
}
}
/// # Safety
///
/// This function is unsafe because improper use may lead to memory
/// problems. For example, a double-free may occur if the function is called
/// twice on the same raw pointer.
#[no_mangle]
pub unsafe extern "C" fn wgpu_client_delete(client: *mut Client) {
log::info!("Terminating WGPU client");
let _client = Box::from_raw(client);
}
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is
/// valid for `id_length` elements.
#[no_mangle]
pub unsafe extern "C" fn wgpu_client_make_adapter_ids(
client: &Client,
@@ -100,6 +109,10 @@ pub unsafe extern "C" fn wgpu_client_make_adapter_ids(
id_length - ids.len()
}
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is
/// valid for `id_length` elements.
#[no_mangle]
pub unsafe extern "C" fn wgpu_client_kill_adapter_ids(
client: &Client,

View File

@@ -14,6 +14,11 @@ pub extern "C" fn wgpu_server_new() -> *mut Global {
Box::into_raw(Box::new(Global::new("wgpu")))
}
/// # Safety
///
/// This function is unsafe because improper use may lead to memory
/// problems. For example, a double-free may occur if the function is called
/// twice on the same raw pointer.
#[no_mangle]
pub unsafe extern "C" fn wgpu_server_delete(global: *mut Global) {
log::info!("Terminating WGPU server");
@@ -25,6 +30,11 @@ pub unsafe extern "C" fn wgpu_server_delete(global: *mut Global) {
/// Provide the list of IDs to pick from.
///
/// Returns the index in this list, or -1 if unable to pick.
///
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is
/// valid for `id_length` elements.
#[no_mangle]
pub unsafe extern "C" fn wgpu_server_instance_request_adapter(
global: &Global,
@@ -67,6 +77,10 @@ pub extern "C" fn wgpu_server_device_create_buffer(
gfx_select!(self_id => global.device_create_buffer(self_id, desc, new_id));
}
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is
/// valid for `size` elements.
#[no_mangle]
pub unsafe extern "C" fn wgpu_server_device_set_buffer_sub_data(
global: &Global,
@@ -80,6 +94,10 @@ pub unsafe extern "C" fn wgpu_server_device_set_buffer_sub_data(
gfx_select!(self_id => global.device_set_buffer_sub_data(self_id, buffer_id, offset, slice));
}
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is
/// valid for `size` elements.
#[no_mangle]
pub unsafe extern "C" fn wgpu_server_device_get_buffer_sub_data(
global: &Global,
@@ -118,6 +136,10 @@ pub extern "C" fn wgpu_server_encoder_destroy(
gfx_select!(self_id => global.command_encoder_destroy(self_id));
}
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is
/// valid for `byte_length` elements.
#[no_mangle]
pub unsafe extern "C" fn wgpu_server_encode_compute_pass(
global: &Global,
@@ -129,6 +151,11 @@ pub unsafe extern "C" fn wgpu_server_encode_compute_pass(
gfx_select!(self_id => global.command_encoder_run_compute_pass(self_id, raw_data));
}
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointers are
/// valid for `color_attachments_length` and `command_length` elements,
/// respectively.
#[no_mangle]
pub unsafe extern "C" fn wgpu_server_encode_render_pass(
global: &Global,
@@ -144,6 +171,10 @@ pub unsafe extern "C" fn wgpu_server_encode_render_pass(
gfx_select!(self_id => global.command_encoder_run_render_pass(self_id, color_attachments, depth_stencil_attachment, raw_pass));
}
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is
/// valid for `command_buffer_id_length` elements.
#[no_mangle]
pub unsafe extern "C" fn wgpu_server_queue_submit(
global: &Global,