Expose a single trait composed of multiple traits?

Hello everyone!

Is there a way to expose a single trait composed of multiple traits?

It would looks like this:

trait Rgb {};

trait Alpha {};

pub trait Rgba<Rgb, Alpha>;

How can I achieve this?

I tried to find more information about this. Is there a name for this? Trait aggregation? Trait composition?

A big thanks in advance, keep the good work! :slight_smile:

Edit: To be more specifi, the final exposition would looks like this:

pub trait Rgb {};

trait Alpha {};

pub trait Rgba<Rgb, Alpha>;

So I want to have Rgb and Rgba trait exposed, but not Alpha.

You can try something like this:

pub trait Rgb {}
mod sealed {
    pub trait Alpha {}
pub trait Rgba: Rgb + sealed::Alpha {}
1 Like

Thanks, it kinda work.

The problem appears when I use a function of the Alpha trait. Lets suppose we have this:

mod sealed {
    pub trait Alpha {
        fn alpha() -> f32;

And I implement alpha() elsewhere, when I try to use this function (in my tests, for example), I get a:

11 |     assert_eq!(col.alpha(), 255);
   |                    ^^^^^ method not found in `u32`
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
1  | use color::rgb_trait::sealed::AlphaTrait;

And of course, I have this in my tests:

use color::RgbTrait;
use color::RgbaTrait;

So RgbaTrait doesn't seems to implicitly expose AlphaTrait. :frowning:

Any idea?

Thanks in advance! :slight_smile:

I think you would need to put the method in the RgbaTrait since that’s the only one that’s accessible outside its modules but not sure how helpful that is in your case.

It seems like then if you don’t want to expose an Alpha trait, I would just remove it and just use the RgbaTrait to include the Alpha related methods.


If the tests are within your crate then you should be able to import Alpha in the test module or function, at which point the alpha method will be accessible. You might have to add pub(crate) on mod sealed, depending on how things are structured. But @drewkett has a point that you might want to just get rid of Alpha entirely.

That's right, you can call trait methods only if the corresponding trait is visible; subtraits like Rgba don't count. This allows you to use private traits as implementation details without implicitly making them part of your library's observable behavior.

1 Like

Thanks, I did this for this case. :slight_smile: