Skip to content

Commit

Permalink
Simplify SDL errors
Browse files Browse the repository at this point in the history
The old SDL bindings I was using didn't expose SDL_GetError as it supposedly wasn't thread-safe, but the SDL3 docs say it is.
  • Loading branch information
17cupsofcoffee committed Dec 2, 2024
1 parent 4b6bfb2 commit 7bca05e
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::mem::MaybeUninit;
use std::sync::atomic::{AtomicBool, Ordering};

use sdl3_sys::error::*;
use sdl3_sys::events::*;
use sdl3_sys::init::*;
use sdl3_sys::stdinc::*;
use sdl3_sys::version::*;
use sdl3_sys::video::*;

Expand Down Expand Up @@ -146,22 +145,10 @@ impl Drop for Window {
}

pub(crate) unsafe fn get_err() -> String {
let mut v: Vec<u8> = Vec::with_capacity(1024);
SDL_strlcpy(v.as_mut_ptr().cast(), SDL_GetError(), v.capacity());

let mut len = 0;
let mut p = v.as_mut_ptr();

while *p != 0 && len <= v.capacity() {
p = p.add(1);
len += 1;
}

v.set_len(len);

match String::from_utf8(v) {
Ok(s) => s,
Err(e) => String::from_utf8_lossy(e.as_bytes()).into_owned(),
unsafe {
CStr::from_ptr(SDL_GetError())
.to_string_lossy()
.into_owned()
}
}

Expand Down

0 comments on commit 7bca05e

Please sign in to comment.