Browse Source

perf: ShortCode impl for rust codec

legacy
Dnomd343 2 years ago
parent
commit
3773a0a166
  1. 16
      src/rust_ffi/src/codec/common_code.rs
  2. 19
      src/rust_ffi/src/codec/mod.rs
  3. 64
      src/rust_ffi/src/codec/short_code.rs

16
src/rust_ffi/src/codec/common_code.rs

@ -0,0 +1,16 @@
use super::ffi as codec_ffi;
#[derive(Debug)]
pub struct CommonCode {
code: u64
}
impl CommonCode {
pub(crate) fn new(common_code: u64) -> CommonCode {
CommonCode {
code: common_code
}
}
}

19
src/rust_ffi/src/codec/mod.rs

@ -1,4 +1,21 @@
mod ffi; mod ffi;
mod short_code; mod short_code;
mod common_code;
pub use short_code::demo; use short_code::ShortCode;
use common_code::CommonCode;
pub fn demo() {
let s = ShortCode::from(12345678).unwrap();
println!("{:?}", s);
println!("{}", s);
println!("{}", s.to_string());
let s = ShortCode::from_str("CSSBF").unwrap();
println!("{}", s.unwrap());
println!("ShortCode warm up");
ShortCode::warm_up();
println!("ShortCode warm up fast");
ShortCode::warm_up_fast();
}

64
src/rust_ffi/src/codec/short_code.rs

@ -1,4 +1,5 @@
use std::fmt; use std::fmt;
use super::CommonCode;
use super::ffi as codec_ffi; use super::ffi as codec_ffi;
#[derive(Debug)] #[derive(Debug)]
@ -6,8 +7,22 @@ pub struct ShortCode {
code: u32 code: u32
} }
impl Eq for ShortCode {}
impl PartialEq for ShortCode {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}
impl fmt::Display for ShortCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}({})", self.to_string(), self.code)
}
}
impl ShortCode { impl ShortCode {
fn from(short_code: u32) -> Result<ShortCode, &'static str> { pub fn from(short_code: u32) -> Result<ShortCode, &'static str> {
match codec_ffi::short_code_check(short_code) { match codec_ffi::short_code_check(short_code) {
true => Ok(ShortCode { true => Ok(ShortCode {
code: short_code code: short_code
@ -16,54 +31,33 @@ impl ShortCode {
} }
} }
fn from_str(short_code: &str) -> Result<ShortCode, &'static str> { pub fn from_str(short_code: &str) -> Result<ShortCode, &'static str> {
Ok(ShortCode { Ok(ShortCode {
code: codec_ffi::short_code_from_string(short_code)? code: codec_ffi::short_code_from_string(short_code)?
}) })
} }
fn to_string(&self) -> String { // TODO: from CommonCode (u32 / String / CommonCode)
pub fn to_string(&self) -> String {
codec_ffi::short_code_to_string_unsafe(self.code) codec_ffi::short_code_to_string_unsafe(self.code)
} }
fn unwrap(&self) -> u32 { pub fn to_common_code(&self) -> CommonCode {
CommonCode::new(
codec_ffi::short_code_to_common_code_unsafe(self.code)
)
}
pub fn unwrap(&self) -> u32 {
self.code self.code
} }
fn warm_up() { pub fn warm_up() {
codec_ffi::short_code_enable(); codec_ffi::short_code_enable();
} }
fn warm_up_fast() { pub fn warm_up_fast() {
codec_ffi::short_code_enable_fast(); codec_ffi::short_code_enable_fast();
} }
// TODO: to CommonCode
// TODO: from CommonCode (u32 / String / CommonCode)
}
impl fmt::Display for ShortCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO: using ShortCode string output
write!(f, "{}", self.code)
}
}
impl PartialEq for ShortCode {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}
impl Eq for ShortCode {}
pub fn demo() {
let s1 = ShortCode::from(12345).unwrap();
let s2 = ShortCode::from(54321).unwrap();
// println!("ok -> {:?}", s);
// println!("ok -> {}", s.to_string());
} }

Loading…
Cancel
Save