Last active
August 29, 2015 13:56
-
-
Save quux00/9298369 to your computer and use it in GitHub Desktop.
How to import C enum into Rust?
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
// Rust code | |
use std::libc; | |
use std::ptr; | |
struct memcached_st; | |
struct memcached_server_st; | |
struct memcached_server_list_st; | |
type in_port_t = i32; | |
#[repr(C)] | |
type memcached_return_t = i32; | |
#[link(name = "memcached")] | |
extern { | |
fn memcached_create(ptr: *memcached_st) -> *memcached_st; | |
fn memcached_server_list_append(list: *memcached_server_list_st, | |
hostname: *libc::c_char, | |
port: in_port_t, | |
error: *memcached_return_t) -> memcached_server_list_st; | |
} | |
fn main() { | |
unsafe { | |
let memc = memcached_create(ptr::null()); | |
println!("{:?}", memc); | |
let rc: memcached_return_t = 0; | |
let servers = memcached_server_list_append(ptr::null(), "127.0.0.1".to_c_str().unwrap(), | |
11211, &rc); | |
println!("{:?}", servers); | |
if rc == MEMCACHED_SUCCESS { // error => no idea what you're talking about' | |
println("Connected to server"); | |
} else { | |
println("Connection failed"); | |
} | |
} | |
} | |
////////////////////////////////////////// | |
////////////////////////////////////////// | |
// from libmemcached C code (return.h) | |
enum memcached_return_t { | |
MEMCACHED_SUCCESS, | |
MEMCACHED_FAILURE, | |
MEMCACHED_HOST_LOOKUP_FAILURE, // getaddrinfo() and getnameinfo() only | |
MEMCACHED_CONNECTION_FAILURE, | |
MEMCACHED_CONNECTION_BIND_FAILURE, // DEPRECATED, see MEMCACHED_HOST_LOOKUP_FAILURE | |
MEMCACHED_WRITE_FAILURE, | |
MEMCACHED_READ_FAILURE, | |
MEMCACHED_UNKNOWN_READ_FAILURE, | |
MEMCACHED_PROTOCOL_ERROR, | |
MEMCACHED_CLIENT_ERROR, | |
MEMCACHED_SERVER_ERROR, // Server returns "SERVER_ERROR" | |
MEMCACHED_ERROR, // Server returns "ERROR" | |
MEMCACHED_DATA_EXISTS, | |
MEMCACHED_DATA_DOES_NOT_EXIST, | |
MEMCACHED_NOTSTORED, | |
MEMCACHED_STORED, | |
MEMCACHED_NOTFOUND, | |
MEMCACHED_MEMORY_ALLOCATION_FAILURE, | |
MEMCACHED_PARTIAL_READ, | |
MEMCACHED_SOME_ERRORS, | |
MEMCACHED_NO_SERVERS, | |
MEMCACHED_END, | |
MEMCACHED_DELETED, | |
MEMCACHED_VALUE, | |
MEMCACHED_STAT, | |
MEMCACHED_ITEM, | |
MEMCACHED_ERRNO, | |
MEMCACHED_FAIL_UNIX_SOCKET, // DEPRECATED | |
MEMCACHED_NOT_SUPPORTED, | |
MEMCACHED_NO_KEY_PROVIDED, /* Deprecated. Use MEMCACHED_BAD_KEY_PROVIDED! */ | |
MEMCACHED_FETCH_NOTFINISHED, | |
MEMCACHED_TIMEOUT, | |
MEMCACHED_BUFFERED, | |
MEMCACHED_BAD_KEY_PROVIDED, | |
MEMCACHED_INVALID_HOST_PROTOCOL, | |
MEMCACHED_SERVER_MARKED_DEAD, | |
MEMCACHED_UNKNOWN_STAT_KEY, | |
MEMCACHED_E2BIG, | |
MEMCACHED_INVALID_ARGUMENTS, | |
MEMCACHED_KEY_TOO_BIG, | |
MEMCACHED_AUTH_PROBLEM, | |
MEMCACHED_AUTH_FAILURE, | |
MEMCACHED_AUTH_CONTINUE, | |
MEMCACHED_PARSE_ERROR, | |
MEMCACHED_PARSE_USER_ERROR, | |
MEMCACHED_DEPRECATED, | |
MEMCACHED_IN_PROGRESS, | |
MEMCACHED_SERVER_TEMPORARILY_DISABLED, | |
MEMCACHED_SERVER_MEMORY_ALLOCATION_FAILURE, | |
MEMCACHED_MAXIMUM_RETURN, /* Always add new error code before */ | |
MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE= MEMCACHED_ERROR | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment