共享篋:基于封装的静态调度实现

  在这一节里,列出基于指针类型Box封装的静态调度实现。

学习内容

  • 了解和学习基于指针类型Box封装的静态函数实现方法

篇目

静态模块实现

  

// File: src/mod_static_fn.rs
use super::mod_trait::TraitCanal;

pub fn get_static_box_ref<Type: TraitCanal>(typ: &Box<Type>) -> (u32) {
    (typ.get_tuple())
}

pub fn get_static_box<Type: TraitCanal>(typ: Box<Type>) -> (u32) {
    (typ.get_tuple())
}

pub fn get_static_box_type_ref<Type: TraitCanal + ?Sized>(typ: Box<&Type>) -> (u32) {
    (typ.get_tuple())
}

pub fn get_static_box_and_type_ref<Type: TraitCanal + ?Sized>(typ: &Box<&Type>) -> (u32) {
    (typ.get_tuple())
}


静态模块应用

use mod_trait_exerci::mod_box_static_fn;
use mod_trait_exerci::mod_trait::StructType;
use mod_trait_exerci::mod_trait::TraitCanal;
use mod_trait_exerci::mod_trait::TupleType;

// clear && cargo run --example box_static_hello
fn main() {
    let instance: StructType = Default::default();
    let instance_box_type: Box<StructType> = Box::new(instance);
    assert_eq!((0), mod_box_static_fn::get_static_box_ref(&instance_box_type));
    assert_eq!((0), mod_box_static_fn::get_static_box(instance_box_type));

    let instance: StructType = Default::default();
    let instance_box_type: Box<&dyn TraitCanal> = Box::new(&instance);
    assert_eq!((0), mod_box_static_fn::get_static_box_and_type_ref(&instance_box_type));
    assert_eq!((0), mod_box_static_fn::get_static_box_type_ref(instance_box_type));

    let instance: TupleType = Default::default();
    let instance_box_type: Box<TupleType> = Box::new(instance);
    assert_eq!((0), mod_box_static_fn::get_static_box_ref(&instance_box_type));
    assert_eq!((0), mod_box_static_fn::get_static_box(instance_box_type));

    let instance: TupleType = Default::default();
    let instance_box_type: Box<&dyn TraitCanal> = Box::new(&instance);
    assert_eq!((0), mod_box_static_fn::get_static_box_and_type_ref(&instance_box_type));
    assert_eq!((0), mod_box_static_fn::get_static_box_type_ref(instance_box_type));
}

参考资料