Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy lints #71

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl FileSystem {
}

pub fn listfiles(&self, path: &str, show_invisible: bool) -> Result<Vec<String>, Error> {
let mut files: Box<Vec<String>> = Box::new(Vec::new());
let mut files: Box<Vec<String>> = Box::default();
let files_ptr: *mut Vec<String> = &mut *files;
let c_path = CString::new(path).map_err(Error::msg)?;
let result = pd_func_caller!(
Expand Down Expand Up @@ -99,7 +99,7 @@ impl FileSystem {
let c_path = CString::new(path).map_err(Error::msg)?;
let raw_file = pd_func_caller!((*self.0).open, c_path.as_ptr(), options)?;
ensure!(
raw_file != ptr::null_mut(),
!raw_file.is_null(),
"Failed to open file at {} with options {:?}",
path,
options
Expand All @@ -109,11 +109,10 @@ impl FileSystem {

pub fn read_file_as_string(&self, path: &str) -> Result<String, Error> {
let stat = self.stat(path)?;
let mut buffer = Vec::with_capacity(stat.size as usize);
buffer.resize(stat.size as usize, 0);
let mut buffer = alloc::vec![0; stat.size as usize];
let sd_file = self.open(path, FileOptions::kFileRead | FileOptions::kFileReadData)?;
sd_file.read(&mut buffer)?;
Ok(String::from_utf8(buffer).map_err(Error::msg)?)
String::from_utf8(buffer).map_err(Error::msg)
}
}

Expand Down
53 changes: 23 additions & 30 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl BitmapInner {
width,
height,
rowbytes,
hasmask: mask_ptr != ptr::null_mut(),
hasmask: !mask_ptr.is_null(),
})
}

Expand All @@ -88,7 +88,7 @@ impl BitmapInner {
self.raw_bitmap,
location.x,
location.y,
flip.into(),
flip,
)?;
Ok(())
}
Expand Down Expand Up @@ -153,7 +153,7 @@ impl BitmapInner {
location.y,
size.width,
size.height,
flip.into(),
flip,
)?;
Ok(())
}
Expand Down Expand Up @@ -213,7 +213,7 @@ impl BitmapInner {
self.raw_bitmap,
&mut out_err
)?;
if out_err != ptr::null_mut() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -360,15 +360,15 @@ fn raw_bitmap(bitmap: OptionalBitmap<'_>) -> *mut crankstart_sys::LCDBitmap {
if let Some(bitmap) = bitmap {
bitmap.inner.borrow().raw_bitmap
} else {
ptr::null_mut() as *mut crankstart_sys::LCDBitmap
ptr::null_mut()
}
}

pub struct Font(*mut crankstart_sys::LCDFont);

impl Font {
pub fn new(font: *mut crankstart_sys::LCDFont) -> Result<Self, Error> {
anyhow::ensure!(font != ptr::null_mut(), "Null pointer passed to Font::new");
anyhow::ensure!(!font.is_null(), "Null pointer passed to Font::new");
Ok(Self(font))
}
}
Expand Down Expand Up @@ -396,7 +396,7 @@ impl BitmapTableInner {
index as c_int
)?;
ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Failed to load bitmap {} from table {:?}",
index,
self.raw_bitmap_table
Expand All @@ -417,7 +417,7 @@ impl BitmapTableInner {
self.raw_bitmap_table,
&mut out_err
)?;
if out_err != ptr::null_mut() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -483,7 +483,7 @@ impl Graphics {

/// Allows drawing directly into an image rather than the framebuffer, for example for
/// drawing text into a sprite's image.
pub fn with_context<F, T>(&self, bitmap: &mut Bitmap, f: F) -> Result<T, Error>
pub fn with_context<F, T>(&self, bitmap: &Bitmap, f: F) -> Result<T, Error>
where
F: FnOnce() -> Result<T, Error>,
{
Expand All @@ -507,28 +507,22 @@ impl Graphics {

pub fn get_frame(&self) -> Result<&'static mut [u8], Error> {
let ptr = pd_func_caller!((*self.0).getFrame)?;
anyhow::ensure!(
ptr != ptr::null_mut(),
"Null pointer returned from getFrame"
);
anyhow::ensure!(!ptr.is_null(), "Null pointer returned from getFrame");
let frame = unsafe { slice::from_raw_parts_mut(ptr, (LCD_ROWSIZE * LCD_ROWS) as usize) };
Ok(frame)
}

pub fn get_display_frame(&self) -> Result<&'static mut [u8], Error> {
let ptr = pd_func_caller!((*self.0).getDisplayFrame)?;
anyhow::ensure!(
ptr != ptr::null_mut(),
"Null pointer returned from getDisplayFrame"
);
anyhow::ensure!(!ptr.is_null(), "Null pointer returned from getDisplayFrame");
let frame = unsafe { slice::from_raw_parts_mut(ptr, (LCD_ROWSIZE * LCD_ROWS) as usize) };
Ok(frame)
}

pub fn get_debug_bitmap(&self) -> Result<Bitmap, Error> {
let raw_bitmap = pd_func_caller!((*self.0).getDebugBitmap)?;
anyhow::ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Null pointer returned from getDebugImage"
);
Ok(Bitmap::new(raw_bitmap, false))
Expand All @@ -537,14 +531,14 @@ impl Graphics {
pub fn get_framebuffer_bitmap(&self) -> Result<Bitmap, Error> {
let raw_bitmap = pd_func_caller!((*self.0).copyFrameBufferBitmap)?;
anyhow::ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Null pointer returned from getFrameBufferBitmap"
);
Ok(Bitmap::new(raw_bitmap, true))
}

pub fn set_background_color(&self, color: LCDSolidColor) -> Result<(), Error> {
pd_func_caller!((*self.0).setBackgroundColor, color.into())
pd_func_caller!((*self.0).setBackgroundColor, color)
}

pub fn set_draw_mode(&self, mode: LCDBitmapDrawMode) -> Result<(), Error> {
Expand Down Expand Up @@ -572,7 +566,7 @@ impl Graphics {
bg_color.into()
)?;
anyhow::ensure!(
raw_bitmap != ptr::null_mut(),
!raw_bitmap.is_null(),
"Null pointer returned from new_bitmap"
);
Ok(Bitmap::new(raw_bitmap, true))
Expand All @@ -582,8 +576,8 @@ impl Graphics {
let c_path = CString::new(path).map_err(Error::msg)?;
let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut();
let raw_bitmap = pd_func_caller!((*self.0).loadBitmap, c_path.as_ptr(), &mut out_err)?;
if raw_bitmap == ptr::null_mut() {
if out_err != ptr::null_mut() {
if raw_bitmap.is_null() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -612,8 +606,8 @@ impl Graphics {
let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut();
let raw_bitmap_table =
pd_func_caller!((*self.0).loadBitmapTable, c_path.as_ptr(), &mut out_err)?;
if raw_bitmap_table == ptr::null_mut() {
if out_err != ptr::null_mut() {
if raw_bitmap_table.is_null() {
if !out_err.is_null() {
let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() };
Err(anyhow!(err_msg))
} else {
Expand Down Expand Up @@ -657,14 +651,13 @@ impl Graphics {
let n_pts = coords.len();
let mut coords_seq = coords
.iter()
.map(|pt| [pt.x, pt.y])
.flatten()
.flat_map(|pt| [pt.x, pt.y])
.collect::<alloc::vec::Vec<_>>();

pd_func_caller!(
(*self.0).fillPolygon,
n_pts as i32,
coords_seq.as_mut_ptr() as *mut i32,
coords_seq.as_mut_ptr(),
color.into(),
fillrule
)?;
Expand Down Expand Up @@ -787,7 +780,7 @@ impl Graphics {
pd_func_caller!(
(*self.0).drawText,
c_text.as_ptr() as *const core::ffi::c_void,
text.len() as usize,
text.len(),
PDStringEncoding::kUTF8Encoding,
position.x,
position.y,
Expand All @@ -800,7 +793,7 @@ impl Graphics {
(*self.0).getTextWidth,
font.0,
c_text.as_ptr() as *const core::ffi::c_void,
text.len() as usize,
text.len(),
PDStringEncoding::kUTF8Encoding,
tracking,
)
Expand Down
61 changes: 26 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ impl Playdate {
sprite_update: SpriteUpdateFunction,
sprite_draw: SpriteDrawFunction,
) -> Result<Self, Error> {
let system = unsafe { (*playdate).system };
let playdate_api = unsafe { *playdate };
let system = playdate_api.system;
System::new(system);
let playdate_sprite = unsafe { (*playdate).sprite };
let playdate_sprite = playdate_api.sprite;
SpriteManager::new(playdate_sprite, sprite_update, sprite_draw);
let file = unsafe { (*playdate).file };
let file = playdate_api.file;
FileSystem::new(file);
let graphics = unsafe { (*playdate).graphics };
let graphics = playdate_api.graphics;
Graphics::new(graphics);
let lua = unsafe { (*playdate).lua };
let lua = playdate_api.lua;
Lua::new(lua);
let sound = unsafe { (*playdate).sound };
let sound = playdate_api.sound;
Sound::new(sound)?;
let display = unsafe { (*playdate).display };
let display = playdate_api.display;
Display::new(display);
Ok(Self { playdate })
}
Expand Down Expand Up @@ -92,7 +93,7 @@ macro_rules! pd_func_caller_log {
if let Some(raw_fn) = $raw_fn_opt {
raw_fn($($arg)*);
} else {
crate::log_to_console!("{} did not contain a function pointer", stringify!($raw_fn_opt));
$crate::log_to_console!("{} did not contain a function pointer", stringify!($raw_fn_opt));
}
}
};
Expand Down Expand Up @@ -149,25 +150,17 @@ impl<T: 'static + Game> GameRunner<T> {
}

if let Some(game) = self.game.as_mut() {
match game.update(&mut self.playdate) {
Err(err) => log_to_console!("Error in update: {}", err),
_ => (),
if let Err(err) = game.update(&mut self.playdate) {
log_to_console!("Error in update: {}", err)
}
if game.draw_and_update_sprites() {
match SpriteManager::get_mut().update_and_draw_sprites() {
Err(err) => {
log_to_console!(
"Error from sprite_manager.update_and_draw_sprites: {}",
err
)
}
_ => (),
if let Err(err) = SpriteManager::get_mut().update_and_draw_sprites() {
log_to_console!("Error from sprite_manager.update_and_draw_sprites: {}", err)
}
}
if game.draw_fps() {
match System::get().draw_fps(0, 0) {
Err(err) => log_to_console!("Error from system().draw_fps: {}", err),
_ => (),
if let Err(err) = System::get().draw_fps(0, 0) {
log_to_console!("Error from system().draw_fps: {}", err)
}
}
} else {
Expand All @@ -179,9 +172,8 @@ impl<T: 'static + Game> GameRunner<T> {
pub fn update_sprite(&mut self, sprite: *mut LCDSprite) {
if let Some(game) = self.game.as_mut() {
if let Some(mut sprite) = SpriteManager::get_mut().get_sprite(sprite) {
match game.update_sprite(&mut sprite, &mut self.playdate) {
Err(err) => log_to_console!("Error in update_sprite: {}", err),
_ => (),
if let Err(err) = game.update_sprite(&mut sprite, &mut self.playdate) {
log_to_console!("Error in update_sprite: {}", err)
}
} else {
log_to_console!("Can't find sprite {:?} to update", sprite);
Expand All @@ -194,9 +186,8 @@ impl<T: 'static + Game> GameRunner<T> {
pub fn draw_sprite(&mut self, sprite: *mut LCDSprite, bounds: PDRect, draw_rect: PDRect) {
if let Some(game) = self.game.as_ref() {
if let Some(sprite) = SpriteManager::get_mut().get_sprite(sprite) {
match game.draw_sprite(&sprite, &bounds, &draw_rect, &self.playdate) {
Err(err) => log_to_console!("Error in draw_sprite: {}", err),
_ => (),
if let Err(err) = game.draw_sprite(&sprite, &bounds, &draw_rect, &self.playdate) {
log_to_console!("Error in draw_sprite: {}", err)
}
} else {
log_to_console!("Can't find sprite {:?} to draw", sprite);
Expand Down Expand Up @@ -342,7 +333,7 @@ unsafe impl Sync for PlaydateAllocator {}
unsafe impl GlobalAlloc for PlaydateAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let system = System::get();
system.realloc(core::ptr::null_mut(), layout.size() as usize) as *mut u8
system.realloc(core::ptr::null_mut(), layout.size()) as *mut u8
}

unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
Expand Down Expand Up @@ -370,7 +361,7 @@ fn alloc_error(_layout: Layout) -> ! {
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
let mut i = 0;
while i < n {
*dest.offset(i as isize) = *src.offset(i as isize);
*dest.add(i) = *src.add(i);
i += 1;
}
dest
Expand All @@ -384,13 +375,13 @@ pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mu
let mut i = n;
while i != 0 {
i -= 1;
*dest.offset(i as isize) = *src.offset(i as isize);
*dest.add(i) = *src.add(i);
}
} else {
// copy from beginning
let mut i = 0;
while i < n {
*dest.offset(i as isize) = *src.offset(i as isize);
*dest.add(i) = *src.add(i);
i += 1;
}
}
Expand All @@ -402,8 +393,8 @@ pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mu
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
let mut i = 0;
while i < n {
let a = *s1.offset(i as isize);
let b = *s2.offset(i as isize);
let a = *s1.add(i);
let b = *s2.add(i);
if a != b {
return a as i32 - b as i32;
}
Expand All @@ -422,7 +413,7 @@ pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
pub unsafe fn memset_internal(s: *mut u8, c: crankstart_sys::ctypes::c_int, n: usize) -> *mut u8 {
let mut i = 0;
while i < n {
*s.offset(i as isize) = c as u8;
*s.add(i) = c as u8;
i += 1;
}
s
Expand Down
Loading