Hi there.
As part of learning rust I have been writing a small image library, and I would like to ask help with a feature I'd like to implement where I can isolate the different color channels and treat each channel as though it were an image of the sub pixel type.
The code below is a small simplified part of the larger thing I'm working on.
In this case Red
is a struct that represent the red channel of an image.
I implement the Image trait for that struct so I can treat Red
like any image where the pixel of that image is just the sub pixel type.
The issue I have is the only way that I have managed to do this is using a trait object reference to the owner of the pixel data.
In some ways I prefer this because if I were using generics and trait bounds users of the library would have to know what the the type is of the concrete owner of the pixel data.
In other words users can simply write this Red<u8>
to say: 'A red channel of a image where the sub pixels are u8 values'. Otherwise they would have to write this Red<Bitmap<RGBA<u8>>>
which is needlessly verbose IMO.
The downside is that this comes with a performance penalty and I was worried that the performance of such a solution would be too much especially if I want to iterate over the pixel data.
It is dynamically dispatching on every pixel call which isn't great.
So my questions are:
- Given the example I have given is this a suitable solution or is it possible to do this with just generic trait bounds?
- What would be the performance implication of such a solution?
As a final thought I thought perhaps that for iterators I could delegate creating an iterator of the sub pixel data to the struct that owns the pixel data, and then pass it back to the caller.
Thank you.