From 3773a0a166e00aa383624c672fc8749a6f1ac7c3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 27 Feb 2023 22:53:32 +0800 Subject: [PATCH] perf: ShortCode impl for rust codec --- src/rust_ffi/src/codec/common_code.rs | 16 +++++++ src/rust_ffi/src/codec/mod.rs | 19 +++++++- src/rust_ffi/src/codec/short_code.rs | 64 ++++++++++++--------------- 3 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 src/rust_ffi/src/codec/common_code.rs diff --git a/src/rust_ffi/src/codec/common_code.rs b/src/rust_ffi/src/codec/common_code.rs new file mode 100644 index 0000000..73beac1 --- /dev/null +++ b/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 + } + } + +} diff --git a/src/rust_ffi/src/codec/mod.rs b/src/rust_ffi/src/codec/mod.rs index 3dc33cb..90613b4 100644 --- a/src/rust_ffi/src/codec/mod.rs +++ b/src/rust_ffi/src/codec/mod.rs @@ -1,4 +1,21 @@ mod ffi; 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(); +} diff --git a/src/rust_ffi/src/codec/short_code.rs b/src/rust_ffi/src/codec/short_code.rs index d5f9e56..a827fc6 100644 --- a/src/rust_ffi/src/codec/short_code.rs +++ b/src/rust_ffi/src/codec/short_code.rs @@ -1,4 +1,5 @@ use std::fmt; +use super::CommonCode; use super::ffi as codec_ffi; #[derive(Debug)] @@ -6,8 +7,22 @@ pub struct ShortCode { 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 { - fn from(short_code: u32) -> Result { + pub fn from(short_code: u32) -> Result { match codec_ffi::short_code_check(short_code) { true => Ok(ShortCode { code: short_code @@ -16,54 +31,33 @@ impl ShortCode { } } - fn from_str(short_code: &str) -> Result { + pub fn from_str(short_code: &str) -> Result { Ok(ShortCode { 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) } - 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 } - fn warm_up() { + pub fn warm_up() { codec_ffi::short_code_enable(); } - fn warm_up_fast() { + pub fn warm_up_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()); - }