Skip to content

Commit

Permalink
Minor cleanup:
Browse files Browse the repository at this point in the history
- Remove use of markers
- use slice::from_raw_buf instead of vec::Vec::from_raw_buf (safer life times)
- get_gamma_ramp had a questionable conversion to a Vec.
  • Loading branch information
csherratt committed Jan 18, 2015
1 parent f57967d commit 938a38f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
4 changes: 2 additions & 2 deletions examples/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::cell::Cell;
fn main() {
let glfw = glfw::init(Some(
glfw::Callback {
f: error_callback as fn(glfw::Error, String, &Cell<uint>),
f: error_callback as fn(glfw::Error, String, &Cell<usize>),
data: Cell::new(0),
}
)).unwrap();
Expand All @@ -35,7 +35,7 @@ fn main() {
let _ = glfw.create_window(300, 300, "Stop it! :(", glfw::WindowMode::Windowed);
}

fn error_callback(_: glfw::Error, description: String, error_count: &Cell<uint>) {
fn error_callback(_: glfw::Error, description: String, error_count: &Cell<usize>) {
error!("GLFW error {:?}: {:?}", error_count.get(), description);
error_count.set(error_count.get() + 1);
}
6 changes: 1 addition & 5 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,14 @@ pub mod monitor {
use libc::{c_int};
use std::cell::RefCell;
use std::mem;
use std::marker;

callback!(
type Args = (monitor: ::Monitor, event: ::MonitorEvent);
type Callback = MonitorCallback;
let ext_set = |&: cb| unsafe { ::ffi::glfwSetMonitorCallback(cb) };
fn callback(monitor: *mut ::ffi::GLFWmonitor, event: c_int) {
let monitor = ::Monitor {
ptr: monitor,
no_copy: marker::NoCopy,
no_send: marker::NoSend,
no_share: marker::NoSync,
ptr: monitor
};
(monitor, mem::transmute(event))
}
Expand Down
3 changes: 3 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ pub type GLFWkeyfun = extern "C" fn(*mut GLFWwindow, c_int, c_int, c
pub type GLFWcharfun = extern "C" fn(*mut GLFWwindow, c_uint);
pub type GLFWmonitorfun = extern "C" fn(*mut GLFWmonitor, c_int);

#[allow(missing_copy_implementations)]
pub enum GLFWmonitor {}

#[allow(missing_copy_implementations)]
pub enum GLFWwindow {}

#[repr(C)]
Expand All @@ -284,6 +286,7 @@ pub struct GLFWgammaramp {

impl Copy for GLFWgammaramp {}

#[allow(missing_copy_implementations)]
#[repr(C)]
pub struct GLFWvidmode {
pub width: c_int,
Expand Down
43 changes: 18 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ use std::ffi::CString;
use std::mem;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::fmt;
use std::marker::{self,Send};
use std::marker::Send;
use std::ptr;
use std::vec;
use std::slice;
use semver::Version;

/// Alias to `MouseButton1`, supplied for improved clarity.
Expand Down Expand Up @@ -475,10 +476,7 @@ impl Glfw {
match unsafe { ffi::glfwGetPrimaryMonitor() } {
ptr if ptr.is_null() => f(None),
ptr => f(Some(&Monitor {
ptr: ptr,
no_copy: marker::NoCopy,
no_send: marker::NoSend,
no_share: marker::NoSync,
ptr: ptr
})),
}
}
Expand All @@ -499,12 +497,9 @@ impl Glfw {
unsafe {
let mut count = 0;
let ptr = ffi::glfwGetMonitors(&mut count);
f(vec::Vec::from_raw_buf(ptr as *const _, count as usize).iter().map(|&ptr| {
f(slice::from_raw_buf(&(ptr as *const _), count as usize).iter().map(|&ptr| {
Monitor {
ptr: ptr,
no_copy: marker::NoCopy,
no_send: marker::NoSend,
no_share: marker::NoSync,
ptr: ptr
}
}).collect::<Vec<Monitor>>().as_slice())
}
Expand Down Expand Up @@ -747,11 +742,9 @@ pub fn get_version_string() -> String {
pub type MonitorCallback<UserData> = Callback<fn(Monitor, MonitorEvent, &UserData), UserData>;

/// A struct that wraps a `*GLFWmonitor` handle.
#[allow(missing_copy_implementations)]
pub struct Monitor {
ptr: *mut ffi::GLFWmonitor,
no_copy: marker::NoCopy,
no_send: marker::NoSend,
no_share: marker::NoSync,
ptr: *mut ffi::GLFWmonitor
}

impl std::fmt::Show for Monitor {
Expand Down Expand Up @@ -791,7 +784,7 @@ impl Monitor {
unsafe {
let mut count = 0;
let ptr = ffi::glfwGetVideoModes(self.ptr, &mut count);
vec::Vec::from_raw_buf(ptr, count as usize).iter().map(VidMode::from_glfw_vid_mode).collect()
slice::from_raw_buf(&ptr, count as usize).iter().map(VidMode::from_glfw_vid_mode).collect()
}
}

Expand All @@ -812,15 +805,18 @@ impl Monitor {
unsafe {
let llramp = *ffi::glfwGetGammaRamp(self.ptr);
GammaRamp {
red: vec::Vec::from_raw_buf(llramp.red as *const _, llramp.size as usize),
green: vec::Vec::from_raw_buf(llramp.green as *const _, llramp.size as usize),
blue: vec::Vec::from_raw_buf(llramp.blue as *const _, llramp.size as usize),
red: slice::from_raw_buf(&(llramp.red as *const c_ushort), llramp.size as usize)
.iter().map(|&x| x).collect(),
green: slice::from_raw_buf(&(llramp.green as *const c_ushort), llramp.size as usize)
.iter().map(|&x| x).collect(),
blue: slice::from_raw_buf(&(llramp.blue as *const c_ushort), llramp.size as usize)
.iter().map(|&x| x).collect(),
}
}
}

/// Wrapper for `glfwSetGammaRamp`.
pub fn set_gamma_ramp(&self, mut ramp: GammaRamp) {
pub fn set_gamma_ramp(&self, ramp: &mut GammaRamp) {
unsafe {
ffi::glfwSetGammaRamp(
self.ptr,
Expand Down Expand Up @@ -1250,10 +1246,7 @@ impl Window {
f(WindowMode::Windowed)
} else {
f(WindowMode::FullScreen(&Monitor {
ptr: ptr,
no_copy: marker::NoCopy,
no_send: marker::NoSend,
no_share: marker::NoSync,
ptr: ptr
}))
}
}
Expand Down Expand Up @@ -1636,7 +1629,7 @@ impl Joystick {
unsafe {
let mut count = 0;
let ptr = ffi::glfwGetJoystickAxes(self.id as c_int, &mut count);
vec::Vec::from_raw_buf(ptr, count as usize).iter().map(|&a| a as f32).collect()
slice::from_raw_buf(&ptr, count as usize).iter().map(|&a| a as f32).collect()
}
}

Expand All @@ -1645,7 +1638,7 @@ impl Joystick {
unsafe {
let mut count = 0;
let ptr = ffi::glfwGetJoystickButtons(self.id as c_int, &mut count);
vec::Vec::from_raw_buf(ptr, count as usize).iter().map(|&b| b as c_int).collect()
slice::from_raw_buf(&ptr, count as usize).iter().map(|&b| b as c_int).collect()
}
}

Expand Down

0 comments on commit 938a38f

Please sign in to comment.