Browse Source

Fix warnings in ctru-rs

pull/10/head
AzureMarker 3 years ago
parent
commit
e6080d0b45
No known key found for this signature in database
GPG Key ID: 47A133F3BF9D03D3
  1. 14
      ctru-rs/src/applets/swkbd.rs
  2. 17
      ctru-rs/src/console.rs
  3. 145
      ctru-rs/src/services/fs.rs
  4. 13
      ctru-rs/src/services/hid.rs
  5. 2
      ctru-rs/src/thread.rs

14
ctru-rs/src/applets/swkbd.rs

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
use std::convert::TryInto;
use std::iter::once;
use std::mem;
use std::mem::MaybeUninit;
use std::str;
use libctru::{
@ -60,8 +60,8 @@ pub enum ValidInput { @@ -60,8 +60,8 @@ pub enum ValidInput {
FixedLen,
}
/// Keyboard feature flags
bitflags! {
/// Keyboard feature flags
pub struct Features: u32 {
const PARENTAL_PIN = 1 << 0;
const DARKEN_TOP_SCREEN = 1 << 1;
@ -75,8 +75,8 @@ bitflags! { @@ -75,8 +75,8 @@ bitflags! {
}
}
/// Keyboard input filtering flags
bitflags! {
/// Keyboard input filtering flags
pub struct Filters: u32 {
const DIGITS = 1 << 0;
const AT = 1 << 1;
@ -92,9 +92,11 @@ impl Swkbd { @@ -92,9 +92,11 @@ impl Swkbd {
/// (from 1-3).
pub fn init(keyboard_type: Kind, num_buttons: i32) -> Self {
unsafe {
let mut state = Box::new(mem::uninitialized::<SwkbdState>());
swkbdInit(state.as_mut(), keyboard_type as u32, num_buttons, -1);
Swkbd { state }
let mut state = MaybeUninit::<SwkbdState>::uninit();
swkbdInit(state.as_mut_ptr(), keyboard_type as u32, num_buttons, -1);
Swkbd {
state: Box::new(state.assume_init()),
}
}
}

17
ctru-rs/src/console.rs

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
use std::default::Default;
use std::mem;
use std::mem::MaybeUninit;
use libctru::{PrintConsole, consoleInit, consoleSelect, consoleClear, consoleSetWindow};
use libctru::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole};
use gfx::Screen;
@ -15,15 +15,20 @@ impl Console { @@ -15,15 +15,20 @@ impl Console {
/// printing.
pub fn init(screen: Screen) -> Self {
unsafe {
let mut context = Box::new(mem::uninitialized::<PrintConsole>());
consoleInit(screen.into(), context.as_mut());
Console { context, }
let mut context = MaybeUninit::<PrintConsole>::uninit();
consoleInit(screen.into(), context.as_mut_ptr());
Console {
context: Box::new(context.assume_init()),
}
}
}
/// Select this console as the current target for stdout
pub fn select(&self) {
unsafe { consoleSelect(self.context.as_ref() as *const _ as *mut _); }
unsafe {
consoleSelect(self.context.as_ref() as *const _ as *mut _);
}
}
/// Clears all text from the console

145
ctru-rs/src/services/fs.rs

@ -3,19 +3,19 @@ @@ -3,19 +3,19 @@
//! This module contains basic methods to manipulate the contents of the 3DS's filesystem.
//! Only the SD card is currently supported.
use std::io::{Read, Write, Seek, SeekFrom};
use std::io::Error as IoError;
use std::io::Result as IoResult;
use std::io::ErrorKind as IoErrorKind;
use std::io::Result as IoResult;
use std::io::{Read, Seek, SeekFrom, Write};
use std::ffi::OsString;
use std::ptr;
use std::slice;
use std::mem;
use std::path::{Path, PathBuf};
use std::ptr;
use std::slice;
use std::sync::Arc;
use widestring::{WideCString, WideCStr};
use widestring::{WideCStr, WideCString};
bitflags! {
#[derive(Default)]
@ -359,7 +359,10 @@ impl File { @@ -359,7 +359,10 @@ impl File {
/// let mut f = File::open(&sdmc_archive, "/foo.txt").unwrap();
/// ```
pub fn open<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<File> {
OpenOptions::new().read(true).archive(arch).open(path.as_ref())
OpenOptions::new()
.read(true)
.archive(arch)
.open(path.as_ref())
}
/// Opens a file in write-only mode.
@ -385,7 +388,11 @@ impl File { @@ -385,7 +388,11 @@ impl File {
/// let mut f = File::create(&sdmc_archive, "/foo.txt").unwrap();
/// ```
pub fn create<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<File> {
OpenOptions::new().write(true).create(true).archive(arch).open(path.as_ref())
OpenOptions::new()
.write(true)
.create(true)
.archive(arch)
.open(path.as_ref())
}
/// Truncates or extends the underlying file, updating the size of this file to become size.
@ -401,7 +408,10 @@ impl File { @@ -401,7 +408,10 @@ impl File {
unsafe {
let r = ::libctru::FSFILE_SetSize(self.handle, size);
if r < 0 {
Err(IoError::new(IoErrorKind::PermissionDenied, ::Error::from(r)))
Err(IoError::new(
IoErrorKind::PermissionDenied,
::Error::from(r),
))
} else {
Ok(())
}
@ -416,9 +426,15 @@ impl File { @@ -416,9 +426,15 @@ impl File {
let mut size = 0;
let r = ::libctru::FSFILE_GetSize(self.handle, &mut size);
if r < 0 {
Err(IoError::new(IoErrorKind::PermissionDenied, ::Error::from(r)))
Err(IoError::new(
IoErrorKind::PermissionDenied,
::Error::from(r),
))
} else {
Ok(Metadata { attributes: 0, size: size })
Ok(Metadata {
attributes: 0,
size: size,
})
}
}
}
@ -431,7 +447,7 @@ impl File { @@ -431,7 +447,7 @@ impl File {
&mut n_read,
self.offset,
buf.as_mut_ptr() as _,
buf.len() as u32
buf.len() as u32,
);
self.offset += n_read as u64;
if r < 0 {
@ -443,7 +459,7 @@ impl File { @@ -443,7 +459,7 @@ impl File {
}
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> IoResult<usize> {
unsafe { read_to_end_uninitialized(self, buf) }
unsafe { read_to_end_uninitialized(self, buf) }
}
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
@ -455,7 +471,7 @@ impl File { @@ -455,7 +471,7 @@ impl File {
self.offset,
buf.as_ptr() as _,
buf.len() as u32,
FsWrite::FS_WRITE_UPDATE_TIME.bits()
FsWrite::FS_WRITE_UPDATE_TIME.bits(),
);
self.offset += n_written as u64;
if r < 0 {
@ -588,13 +604,21 @@ impl OpenOptions { @@ -588,13 +604,21 @@ impl OpenOptions {
let mut file_handle = 0;
let path = to_utf16(path);
let fs_path = ::libctru::fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = ::libctru::FSUSER_OpenFile(&mut file_handle, self.arch_handle,
fs_path, flags.bits(), 0);
let r = ::libctru::FSUSER_OpenFile(
&mut file_handle,
self.arch_handle,
fs_path,
flags.bits(),
0,
);
if r < 0 {
return Err(IoError::new(IoErrorKind::Other, ::Error::from(r)));
}
let mut file = File { handle: file_handle, offset: 0 };
let mut file = File {
handle: file_handle,
offset: 0,
};
if self.append {
let metadata = file.metadata()?;
@ -613,13 +637,14 @@ impl OpenOptions { @@ -613,13 +637,14 @@ impl OpenOptions {
fn get_open_flags(&self) -> FsOpen {
match (self.read, self.write || self.append, self.create) {
(true, false, false) => FsOpen::FS_OPEN_READ,
(false, true, false) => FsOpen::FS_OPEN_WRITE,
(false, true, true) => FsOpen::FS_OPEN_WRITE | FsOpen::FS_OPEN_CREATE,
(true, false, true) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_CREATE,
(true, true, false) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE,
(true, true, true) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE |
FsOpen::FS_OPEN_CREATE,
(true, false, false) => FsOpen::FS_OPEN_READ,
(false, true, false) => FsOpen::FS_OPEN_WRITE,
(false, true, true) => FsOpen::FS_OPEN_WRITE | FsOpen::FS_OPEN_CREATE,
(true, false, true) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_CREATE,
(true, true, false) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE,
(true, true, true) => {
FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE | FsOpen::FS_OPEN_CREATE
}
_ => FsOpen::empty(), //failure case
}
}
@ -637,14 +662,18 @@ impl<'a> Iterator for ReadDir<'a> { @@ -637,14 +662,18 @@ impl<'a> Iterator for ReadDir<'a> {
};
let mut entries_read = 0;
let entry_count = 1;
let r = ::libctru::FSDIR_Read(self.handle.0, &mut entries_read,
entry_count, &mut ret.entry);
let r = ::libctru::FSDIR_Read(
self.handle.0,
&mut entries_read,
entry_count,
&mut ret.entry,
);
if r < 0 {
return Some(Err(IoError::new(IoErrorKind::Other, ::Error::from(r))))
return Some(Err(IoError::new(IoErrorKind::Other, ::Error::from(r))));
}
if entries_read != entry_count {
return None
return None;
}
Some(Ok(ret))
}
@ -688,8 +717,11 @@ pub fn create_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<()> { @@ -688,8 +717,11 @@ pub fn create_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<()> {
unsafe {
let path = to_utf16(path.as_ref());
let fs_path = ::libctru::fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = ::libctru::FSUSER_CreateDirectory(arch.handle, fs_path,
FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits());
let r = ::libctru::FSUSER_CreateDirectory(
arch.handle,
fs_path,
FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits(),
);
if r < 0 {
Err(IoError::new(IoErrorKind::Other, ::Error::from(r)))
} else {
@ -726,8 +758,11 @@ pub fn metadata<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<Metadata> { @@ -726,8 +758,11 @@ pub fn metadata<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<Metadata> {
let maybe_dir = read_dir(&arch, path.as_ref());
match (maybe_file, maybe_dir) {
(Ok(file), _) => file.metadata(),
(_, Ok(_dir)) => Ok(Metadata { attributes: FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits(), size: 0 }),
(Err(e), _) => Err(e),
(_, Ok(_dir)) => Ok(Metadata {
attributes: FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits(),
size: 0,
}),
(Err(e), _) => Err(e),
}
}
@ -792,7 +827,11 @@ pub fn read_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<ReadDir> { @@ -792,7 +827,11 @@ pub fn read_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<ReadDir> {
if r < 0 {
Err(IoError::new(IoErrorKind::Other, ::Error::from(r)))
} else {
Ok(ReadDir { handle: Dir(handle), root: root, arch: arch})
Ok(ReadDir {
handle: Dir(handle),
root: root,
arch: arch,
})
}
}
}
@ -830,9 +869,10 @@ pub fn remove_file<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<()> { @@ -830,9 +869,10 @@ pub fn remove_file<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<()> {
/// * from does not exist.
/// * The user lacks permissions to view contents.
pub fn rename<P, Q>(arch: &Archive, from: P, to: Q) -> IoResult<()>
where P: AsRef<Path>,
Q: AsRef<Path> {
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
unsafe {
let from = to_utf16(from.as_ref());
let to = to_utf16(to.as_ref());
@ -842,11 +882,11 @@ pub fn rename<P, Q>(arch: &Archive, from: P, to: Q) -> IoResult<()> @@ -842,11 +882,11 @@ pub fn rename<P, Q>(arch: &Archive, from: P, to: Q) -> IoResult<()>
let r = ::libctru::FSUSER_RenameFile(arch.handle, fs_from, arch.handle, fs_to);
if r == 0 {
return Ok(())
return Ok(());
}
let r = ::libctru::FSUSER_RenameDirectory(arch.handle, fs_from, arch.handle, fs_to);
if r == 0 {
return Ok(())
return Ok(());
}
Err(IoError::new(IoErrorKind::Other, ::Error::from(r)))
}
@ -862,7 +902,7 @@ fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { @@ -862,7 +902,7 @@ fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
match v.iter().position(|c| *c == 0) {
// don't include the 0
Some(i) => &v[..i],
None => v
None => v,
}
}
@ -878,7 +918,7 @@ fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { @@ -878,7 +918,7 @@ fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
// Implementations using this method have to adhere to two guarantees:
// * The implementation of read never reads the buffer provided.
// * The implementation of read correctly reports how many bytes were written.
unsafe fn read_to_end_uninitialized(r: &mut Read, buf: &mut Vec<u8>) -> IoResult<usize> {
unsafe fn read_to_end_uninitialized(r: &mut dyn Read, buf: &mut Vec<u8>) -> IoResult<usize> {
let start_len = buf.len();
buf.reserve(16);
@ -893,14 +933,23 @@ unsafe fn read_to_end_uninitialized(r: &mut Read, buf: &mut Vec<u8>) -> IoResult @@ -893,14 +933,23 @@ unsafe fn read_to_end_uninitialized(r: &mut Read, buf: &mut Vec<u8>) -> IoResult
buf.reserve(1);
}
let buf_slice = slice::from_raw_parts_mut(buf.as_mut_ptr().offset(buf.len() as isize),
buf.capacity() - buf.len());
let buf_slice = slice::from_raw_parts_mut(
buf.as_mut_ptr().offset(buf.len() as isize),
buf.capacity() - buf.len(),
);
match r.read(buf_slice) {
Ok(0) => { return Ok(buf.len() - start_len); }
Ok(n) => { let len = buf.len() + n; buf.set_len(len); },
Err(ref e) if e.kind() == IoErrorKind::Interrupted => { }
Err(e) => { return Err(e); }
Ok(0) => {
return Ok(buf.len() - start_len);
}
Ok(n) => {
let len = buf.len() + n;
buf.set_len(len);
}
Err(ref e) if e.kind() == IoErrorKind::Interrupted => {}
Err(e) => {
return Err(e);
}
}
}
}
@ -930,17 +979,17 @@ impl Seek for File { @@ -930,17 +979,17 @@ impl Seek for File {
match pos {
SeekFrom::Start(off) => {
self.offset = off;
},
}
SeekFrom::End(off) => {
let mut temp = self.metadata()?.len() as i64;
temp += off;
self.offset = temp as u64;
},
}
SeekFrom::Current(off) => {
let mut temp = self.offset as i64;
temp += off;
self.offset = temp as u64;
},
}
}
Ok(self.offset)
}

13
ctru-rs/src/services/hid.rs

@ -4,10 +4,9 @@ @@ -4,10 +4,9 @@
//! and circle pad information. It also provides information from the sound volume slider,
//! the accelerometer, and the gyroscope.
/// A set of flags corresponding to the button and directional pad
/// inputs on the 3DS
bitflags! {
/// A set of flags corresponding to the button and directional pad
/// inputs on the 3DS
#[derive(Default)]
pub struct KeyPad: u32 {
const KEY_A = 1u32 << 0;
@ -115,7 +114,9 @@ impl TouchPosition { @@ -115,7 +114,9 @@ impl TouchPosition {
/// Returns the current touch position in pixels.
pub fn get(&mut self) -> (u16, u16) {
unsafe { ::libctru::hidTouchRead(&mut self.0); }
unsafe {
::libctru::hidTouchRead(&mut self.0);
}
(self.0.px, self.0.py)
}
}
@ -128,7 +129,9 @@ impl CirclePosition { @@ -128,7 +129,9 @@ impl CirclePosition {
/// Returns the current circle pad position in (x, y) form.
pub fn get(&mut self) -> (i16, i16) {
unsafe { ::libctru::hidCircleRead(&mut self.0); }
unsafe {
::libctru::hidCircleRead(&mut self.0);
}
(self.0.dx, self.0.dy)
}
}

2
ctru-rs/src/thread.rs

@ -724,7 +724,7 @@ impl fmt::Debug for Thread { @@ -724,7 +724,7 @@ impl fmt::Debug for Thread {
/// ```
///
/// [`Result`]: ../../std/result/enum.Result.html
pub type Result<T> = ::std::result::Result<T, Box<Any + Send + 'static>>;
pub type Result<T> = ::std::result::Result<T, Box<dyn Any + Send + 'static>>;
// This packet is used to communicate the return value between the child thread
// and the parent thread. Memory is shared through the `Arc` within and there's

Loading…
Cancel
Save