mirror of
https://github.com/librespot-org/librespot.git
synced 2026-04-27 08:15:50 +03:00
[GH-ISSUE #688] Create C bindings #392
Labels
No labels
A-Alsa
SpotifyAPI
Tokio 1.0
audio
bug
can't reproduce
compilation
dependencies
duplicate
enhancement
good first issue
help wanted
high priority
imported
imported
invalid
new api
pull-request
question
reverse engineering
wiki
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/librespot#392
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Johannesd3 on GitHub (Apr 11, 2021).
Original GitHub issue: https://github.com/librespot-org/librespot/issues/688
@d4rwel inspired me to write down some thoughts I had about creating C bindings for librespot. There's no need to change anything in librespot itself, it could happen in its own crate/repo. However, I assume that #665 is ready, but this could be accomplished with git dependencies.
So here's a rough plan:
Create a rust library crate and add the following to the
Cargo.toml:Pick some important Rust functions from librespot. Maybe look at the examples to decide what's needed.
Create functions that are callable from C as shown in this example.
Important: Wrap everything into a catch_unwind. If an error occurs, there might be UB if it reaches the border to C.
There are certainly some structs that need to be passed. Since those structs are not compatible with C, they must be passed as opaque type behind a pointer (as in the example).
Some functions are
async. These functions must be executed by a tokio runtime. Create functions to created and destroy a tokio runtime. Return this runtime as opaque struct. (This struct could even be calledlibrespotif the user shouldn't be bothered with implementation details.) For every function call that executes an async rust function, a pointer to this struct must be passed.Remember, destructors in Rust are implicit. It's important to create functions that deallocate resources again, although these functions seem to do nothing. Look at the example above.
If it's ready, running
cargo buildwill produce a.sofile intarget/debug.Run
cargo install cbindgento install a tool to create a C header file.cbindgen --lang cwill hopefully spit out the expected result.You can now write a C application that uses librespot. Just include the header and link it against this
.sofile.How does it sound?