lib.rs 3.2 KB
Newer Older
Z
zhouzj 已提交
1
mod yitgen;
yitter's avatar
yitter 已提交
2

Z
zhouzj 已提交
3 4 5
use yitgen::gen::YitIdHelper;
use yitgen::contract::*;

yitter's avatar
yitter 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#[macro_use]
extern crate lazy_static;
extern crate redis;
extern crate libc;

use redis::Commands;

use libc::{c_char, uint32_t};
use std::ffi::{CStr, CString};
use std::str;

lazy_static! {
        //static ref TestValue: Vec<i32> = vec!(0);
        // static ref MAP: HashMap<u32, String> = HashMap::new();
 }


yitter's avatar
yitter 已提交
23 24 25 26 27 28
// // #[export_name = "SetIdGenerator"]
// #[no_mangle]
// pub extern "C" fn SetIdGenerator(options: IdGeneratorOptions) {
//     YitIdHelper::SetIdGenerator(options);
// }

Z
zhouzj 已提交
29
#[no_mangle]
yitter's avatar
yitter 已提交
30 31 32 33
pub extern "C" fn SetOptions(workerId: u32, workerIdBitLength: u8, seqBitLength: u8) {
    let mut options = IdGeneratorOptions::New(1);
    options.WorkerIdBitLength = workerIdBitLength;
    options.SeqBitLength = seqBitLength;
Z
zhouzj 已提交
34 35 36
    YitIdHelper::SetIdGenerator(options);
}

yitter's avatar
yitter 已提交
37 38 39 40 41
#[no_mangle]
pub extern "C" fn SetWorkerId(workerId: u32) {
    YitIdHelper::SetWorkerId(workerId);
}

Z
zhouzj 已提交
42 43 44 45 46
#[no_mangle]
pub extern "C" fn NextId() -> i64 {
    YitIdHelper::NextId()
}

yitter's avatar
yitter 已提交
47 48 49 50 51 52 53 54
// static mut TestValue: i32 = 0;
// #[no_mangle]
// pub extern "C" fn Test() -> i32 {
//     unsafe {
//         TestValue += 1;
//         return TestValue;
//     }
// }
yitter's avatar
yitter 已提交
55

yitter's avatar
yitter 已提交
56 57 58 59 60 61 62 63 64
// #[no_mangle]
// pub extern "C"
// fn GetWorkerId(ip: *const c_char, port: i32) -> redis::RedisResult<isize> {
//     // let c_str = unsafe {
//     //     assert!(!ip.is_null());
//     //     CStr::from_ptr(ip)
//     // };
//     //
//     // let r_str = c_str.to_str();
yitter's avatar
yitter 已提交
65
//
yitter's avatar
yitter 已提交
66 67 68
//     // connect to redis
//     // let client = redis::Client::open(format!("redis://{}:{}/", String::from(r_str).to_string(), port))?;
//     let client = redis::Client::open(format!("redis://localhost:{}/", port))?;
yitter's avatar
yitter 已提交
69
//
yitter's avatar
yitter 已提交
70 71 72 73 74 75 76 77 78 79
//     let mut con = client.get_connection()?;
//     // throw away the result, just make sure it does not fail
//     unsafe {
//         let _: () = con.set("my_key111", TestValue.clone())?;
//     }
//     con.get("my_key")
// // read back the key and return it.  Because the return value
// // from the function is a result for integer this will automatically
// // convert into one.
// //
yitter's avatar
yitter 已提交
80
//
yitter's avatar
yitter 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
// // match simple_redis::create(&format!("redis://{}:{}/", ip, port)) {
// //     Ok(mut client) => {
// //         println!("Created Redis Client");
// //
// //         let valueString = TestValue.to_string();
// //         let valueString2 = (*TestValue).to_string();
// //
// //         match client.set("my_key", valueString) {
// //             Err(error) => println!("Unable to set value in Redis: {}", error),
// //             _ => println!("Value set in Redis")
// //         };
// //
// //         match client.set("my_key2", valueString2) {
// //             Err(error) => println!("Unable to set value in Redis: {}", error),
// //             _ => println!("Value set in Redis")
// //         };
// //
// //         match client.quit() {
// //             Err(error) => println!("Error: {}", error),
// //             _ => println!("Connection Closed.")
// //         }
// //     }
// //     Err(error) => println!("Unable to create Redis client: {}", error)
// // }
yitter's avatar
yitter 已提交
105
//
yitter's avatar
yitter 已提交
106
// //return 1;
yitter's avatar
yitter 已提交
107
// }
yitter's avatar
yitter 已提交
108 109

// cargo build --release