Search code examples
rustsdl-2rust-cargo

What is the difference between sdl2 and sdl2_sys?


As far as I can tell, sdl2_sys is a crate of automatically generated bindings for all of SDL2, and sdl2 is a crate of bindings based on sdl2_sys, which lacks equivalents to certain functions, specifically IMG_LoadSVG_RW.

I have seen a few sources say that sdl2 is a safe wrapper for SDL2 (here), but I haven't seen anyone say the same for sdl2_sys. I have to write unsafe a lot when using functions from sdl2_sys, which I don't like because it means there is no longer a guarantee that what I write will be safe. Another issue with sdl2_sys is that functions sometimes return c types (so far I've only had to deal with *c_char but I'm sure there are more), which are awkward to convert back. Finally, every tutorial I have found so far is for sdl2, so It is always harder to find out how exactly to do something.

I need to use IMG_LoadSVG_RW, so it's clear that I will have to use sdl2_sys in some way, but I would like to know wether it is better to only use sdl2_sys where I can't use sdl2 and deal with the problems that causes, or to use sdl2_sys for everything and have a significantly worse experience.


Solution

  • In general, the *_sys crate contains (often automatically generated) one-to-one bindings to the C API of the library in question, some Rust settings to link to the library, and nothing else. The main, non-sys crates offer a memory-safe and ergonomic interface to the library, written using the bindings from the sys crate.

    Using sys crates directly is usually not recommended - you may as well just be writing C. However the safe bindings in the non-sys crate are written by third parties who may not have gotten around to safely binding every single bit of functionality in the library, so they may be lacking.