Skip to main content

SerialGenerator

Trait SerialGenerator 

Source
pub trait SerialGenerator: Send + Sync {
    // Required methods
    fn generate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        rule_key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<String, SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn batch_generate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        rule_key: &'life1 str,
        count: u32,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn peek<'life0, 'life1, 'async_trait>(
        &'life0 self,
        rule_key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<String, SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn register_rule<'life0, 'async_trait>(
        &'life0 self,
        rule: SerialRule,
    ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn remove_rule<'life0, 'life1, 'async_trait>(
        &'life0 self,
        rule_key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn enable_rule<'life0, 'life1, 'async_trait>(
        &'life0 self,
        rule_key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn disable_rule<'life0, 'life1, 'async_trait>(
        &'life0 self,
        rule_key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn query_records<'life0, 'life1, 'async_trait>(
        &'life0 self,
        rule_key: &'life1 str,
        page: u64,
        page_size: u64,
    ) -> Pin<Box<dyn Future<Output = Result<(Vec<SerialRecord>, u64), SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn list_rules<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<SerialRule>, SerialError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

单号生成器抽象 trait

三种后端实现共享此接口,遵循 TaskStorage 设计模式:

  • 业务方实现自己的后端(或使用内置的 Memory/Redis/PG 后端)
  • 通过 Arc<dyn SerialGenerator> 注入到 SerialPlugin

§示例

let backend = Arc::new(MemorySerialBackend::new());
backend.register_rule(rule).await?;
let order_no = backend.generate("order").await?;

Required Methods§

Source

fn generate<'life0, 'life1, 'async_trait>( &'life0 self, rule_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String, SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

生成下一个单号

Source

fn batch_generate<'life0, 'life1, 'async_trait>( &'life0 self, rule_key: &'life1 str, count: u32, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

批量生成单号

Source

fn peek<'life0, 'life1, 'async_trait>( &'life0 self, rule_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String, SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

预览下一个单号(不消耗计数器)

Source

fn register_rule<'life0, 'async_trait>( &'life0 self, rule: SerialRule, ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

运行时注册/更新一条规则

Source

fn remove_rule<'life0, 'life1, 'async_trait>( &'life0 self, rule_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

运行时删除一条规则

Source

fn enable_rule<'life0, 'life1, 'async_trait>( &'life0 self, rule_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

运行时启用一条规则

Source

fn disable_rule<'life0, 'life1, 'async_trait>( &'life0 self, rule_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

运行时禁用一条规则

Source

fn query_records<'life0, 'life1, 'async_trait>( &'life0 self, rule_key: &'life1 str, page: u64, page_size: u64, ) -> Pin<Box<dyn Future<Output = Result<(Vec<SerialRecord>, u64), SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

查询生成记录(分页),返回 (记录列表, 总数)

Source

fn list_rules<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<SerialRule>, SerialError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

获取所有已注册的规则

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§