Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-export struct with generics applied

Tags:

rust

graphics

When re-exporting structs from crates, can you also specify some generics?

as in;

// file: transform.rs
pub use euclid::Transform2d as Transform<f32, f32, f32>;

// file: something-else.rs

use transform::Transform;

Transform::new(...); // notice no generics, as its been pre-applied

I know the above isn't valid rust, but that is the idea.

Why? It's for when I want to build an internal api, but do not want to specify this in any other way.

like image 637
Marais Rossouw Avatar asked Nov 30 '25 02:11

Marais Rossouw


1 Answers

You can create a type alias and export it

pub mod module_1 {
    pub struct TypeWithGeneric<T> {
        field: T
    }
    
    pub type PublicType = TypeWithGeneric<u64>;
}

mod module_2 {
    use crate::module_1::PublicType;
    
    fn usage_of_type(_: PublicType) {
    }
}
like image 146
AlexN Avatar answered Dec 01 '25 15:12

AlexN