Skip to content

Instantly share code, notes, and snippets.

@mkomitee
Last active August 29, 2015 14:20
Show Gist options
  • Save mkomitee/17ca562f2429e03f6227 to your computer and use it in GitHub Desktop.
Save mkomitee/17ca562f2429e03f6227 to your computer and use it in GitHub Desktop.
Still no idea
extern crate libc;
#[allow(non_camel_case_types)]
#[repr(C)]
type krb5_error_code = libc::c_int;
#[allow(non_camel_case_types)]
#[repr(C)]
struct krb5_context;
#[derive(Debug)]
struct Krb5Context {
ctx: *mut krb5_context,
}
impl Krb5Context {
fn new() -> Result<Krb5Context, krb5_error_code> {
let rctx = unsafe {
let mut kcontext: *mut krb5_context = std::ptr::null_mut();
let err = krb5_init_context(&mut kcontext);
if err == 0 {
Ok(kcontext)
} else {
Err(err)
}
};
match rctx {
Ok(ctx) => Ok(Krb5Context{ctx: ctx}),
Err(e) => Err(e),
}
}
}
impl Drop for Krb5Context {
fn drop(&mut self) {
println!("Freeing!");
unsafe{
krb5_free_context(self.ctx)
}
}
}
#[link(name = "krb5")]
extern "C" {
fn krb5_init_context(context: *mut *mut krb5_context) -> krb5_error_code;
fn krb5_free_context(context: *mut krb5_context);
}
fn main() {
let kctx = Krb5Context::new();
match kctx {
Ok(ctx) => println!("{:?} YAY!", ctx),
Err(e) => println!("{:?} OHNOES", e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment