Skip to content

IAudioStreamPlayback::mix parameters could be made safe #1130

Open
@djcsdy

Description

@djcsdy

IAudioStreamPlayback::mix is marked unsafe because it takes a raw pointer:

unsafe fn mix(&mut self, buffer: * mut AudioFrame, rate_scale: f32, frames: i32,) -> i32;

The first thing I do when implementing this function is to convert buffer to a slice:

unsafe fn mix(&mut self, buffer: *mut AudioFrame, rate_scale: f32, frames: i32) -> i32 {
    let buffer = unsafe { std::slice::from_raw_parts_mut(buffer, frames as usize) }

    // ... body of function here (can be entirely safe code)

    frames
}

It'd be nice if godot-rust did this conversion for me before calling mix, which could then be made safe:

fn mix(&mut self, buffer: &mut [AudioFrame], rate_scale: f32) -> i32;

Godot calls mix from a non-main thread. I am making the assumption that Godot promises not to read or mutate buffer until mix returns. This seems a safe assumption, because it would be very silly if Godot violated this assumption, but I have not actually checked.

The fact that Godot calls mix from a non-main thread might also be justification on its own for keeping mix marked unsafe, but that's a separate issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking-changeRequires SemVer bumpc: engineGodot classes (nodes, resources, ...)c: ffiLow-level components and interaction with GDExtension APIquality-of-lifeNo new functionality, but improves ergonomics/internals

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions