You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.rs 3.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. mod yitgen;
  2. use yitgen::gen::YitIdHelper;
  3. use yitgen::contract::*;
  4. #[macro_use]
  5. extern crate lazy_static;
  6. // extern crate redis;
  7. extern crate libc;
  8. // use redis::Commands;
  9. use libc::{c_char, uint32_t};
  10. use std::ffi::{CStr, CString};
  11. use std::str;
  12. // lazy_static! {
  13. // static ref TestValue: Vec<i32> = vec!(0);
  14. // static ref MAP: HashMap<u32, String> = HashMap::new();
  15. // }
  16. // #[export_name = "SetIdGenerator"]
  17. #[no_mangle]
  18. pub extern "C" fn SetIdGenerator(options: IdGeneratorOptions) {
  19. YitIdHelper::SetIdGenerator(options);
  20. }
  21. #[no_mangle]
  22. pub extern "C" fn SetOptions(workerId: u32, workerIdBitLength: u8, seqBitLength: u8) {
  23. let mut options = IdGeneratorOptions::New(1);
  24. options.WorkerIdBitLength = workerIdBitLength;
  25. options.SeqBitLength = seqBitLength;
  26. YitIdHelper::SetIdGenerator(options);
  27. }
  28. #[no_mangle]
  29. pub extern "C" fn SetWorkerId(workerId: u32) {
  30. YitIdHelper::SetWorkerId(workerId);
  31. }
  32. #[no_mangle]
  33. pub extern "C" fn NextId() -> i64 {
  34. YitIdHelper::NextId()
  35. }
  36. // static mut TestValue: i32 = 0;
  37. // #[no_mangle]
  38. // pub extern "C" fn Test() -> i32 {
  39. // unsafe {
  40. // TestValue += 1;
  41. // return TestValue;
  42. // }
  43. // }
  44. // #[no_mangle]
  45. // pub extern "C"
  46. // fn GetWorkerId(ip: *const c_char, port: i32) -> redis::RedisResult<isize> {
  47. // // let c_str = unsafe {
  48. // // assert!(!ip.is_null());
  49. // // CStr::from_ptr(ip)
  50. // // };
  51. // //
  52. // // let r_str = c_str.to_str();
  53. //
  54. // // connect to redis
  55. // // let client = redis::Client::open(format!("redis://{}:{}/", String::from(r_str).to_string(), port))?;
  56. // let client = redis::Client::open(format!("redis://localhost:{}/", port))?;
  57. //
  58. // let mut con = client.get_connection()?;
  59. // // throw away the result, just make sure it does not fail
  60. // unsafe {
  61. // let _: () = con.set("my_key111", TestValue.clone())?;
  62. // }
  63. // con.get("my_key")
  64. // // read back the key and return it. Because the return value
  65. // // from the function is a result for integer this will automatically
  66. // // convert into one.
  67. // //
  68. //
  69. // // match simple_redis::create(&format!("redis://{}:{}/", ip, port)) {
  70. // // Ok(mut client) => {
  71. // // println!("Created Redis Client");
  72. // //
  73. // // let valueString = TestValue.to_string();
  74. // // let valueString2 = (*TestValue).to_string();
  75. // //
  76. // // match client.set("my_key", valueString) {
  77. // // Err(error) => println!("Unable to set value in Redis: {}", error),
  78. // // _ => println!("Value set in Redis")
  79. // // };
  80. // //
  81. // // match client.set("my_key2", valueString2) {
  82. // // Err(error) => println!("Unable to set value in Redis: {}", error),
  83. // // _ => println!("Value set in Redis")
  84. // // };
  85. // //
  86. // // match client.quit() {
  87. // // Err(error) => println!("Error: {}", error),
  88. // // _ => println!("Connection Closed.")
  89. // // }
  90. // // }
  91. // // Err(error) => println!("Unable to create Redis client: {}", error)
  92. // // }
  93. //
  94. // //return 1;
  95. // }
  96. // build-win-x64: cargo build --release
  97. // build-linux-x64: cargo build --target x86_64-unknown-linux-musl --release

雪花算法中非常好用的数字ID生成器