From 6f84a43cabb1df04162b76ad824eea11646a1e55 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Sat, 4 Jun 2016 00:47:09 -0700 Subject: [PATCH] Re-introduced RAII semeantics for {Apt, Hid, Sdmc} --- src/sdmc.rs | 2 +- src/services/apt.rs | 20 +++++++++++++++++--- src/services/hid.rs | 19 ++++++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/sdmc.rs b/src/sdmc.rs index 23a70ea..5e98552 100644 --- a/src/sdmc.rs +++ b/src/sdmc.rs @@ -7,7 +7,7 @@ pub struct Sdmc { } impl Sdmc { - pub fn new() -> Result { + pub fn new() -> Result { unsafe { let r = sdmcInit(); if r < 0 { diff --git a/src/services/apt.rs b/src/services/apt.rs index 8c891a6..612fdf7 100644 --- a/src/services/apt.rs +++ b/src/services/apt.rs @@ -51,14 +51,22 @@ impl From for AppStatus { } pub struct Apt { - pd: PhantomData<()>, + pd: PhantomData } impl Apt { - pub fn new() -> Apt { - Apt { pd: PhantomData } + pub fn new() -> Result { + unsafe { + let r = apt::aptInit(); + if r < 0 { + Err(r) + } else { + Ok(Apt { pd: PhantomData }) + } + } } + pub fn get_status(&self) -> AppStatus { unsafe { apt::aptGetStatus().into() } } @@ -88,3 +96,9 @@ impl Apt { } } } + +impl Drop for Apt { + fn drop(&mut self) { + unsafe { apt::aptExit() }; + } +} diff --git a/src/services/hid.rs b/src/services/hid.rs index 14dda12..a9e7954 100644 --- a/src/services/hid.rs +++ b/src/services/hid.rs @@ -74,12 +74,19 @@ impl From for u32 { } pub struct Hid { - pd: PhantomData<()>, + pd: PhantomData } impl Hid { - pub fn new() -> Hid { - Hid { pd: PhantomData } + pub fn new() -> Result { + unsafe { + let r = hid::hidInit(); + if r < 0 { + Err(r) + } else { + Ok(Hid { pd: PhantomData }) + } + } } pub fn scan_input(&mut self) { @@ -119,3 +126,9 @@ impl Hid { } } } + +impl Drop for Hid { + fn drop(&mut self) { + unsafe { hid::hidExit() }; + } +}