Created
October 10, 2012 20:18
-
-
Save ben0x539/3868150 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use libc::{c_void, size_t}; | |
#[nolink] | |
extern mod libc_ { | |
fn qsort(base: *libc::c_void, nmemb: size_t, size: size_t, | |
compar: *u8); // extern fn(a: *c_void, b: *c_void) -> int | |
} | |
fn compare<T: cmp::Ord>(a: *c_void, b: *c_void) -> int unsafe { | |
let (a_, b_) = (&*(a as *T), &*(b as *T)); | |
if *a_ < *b_ { | |
-1 | |
} else if *a_ > *b_ { | |
1 | |
} else { | |
0 | |
} | |
} | |
extern fn compare_str(a: *c_void, b: *c_void) -> int { | |
compare::<~str>(a, b) | |
} | |
fn qsort<T>(v: &[mut T]) { | |
do vec::as_mut_buf(v) |base, nmemb| { | |
libc_::qsort( | |
base as *c_void, | |
nmemb as size_t, | |
sys::size_of::<T>() as size_t, | |
compare_str); | |
} | |
} | |
fn main() { | |
let mut args = os::args(); | |
qsort(args); | |
io::println(fmt!("%?", args)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment