How to do introspection in Rust

I use a library, which is full of generics.
Here is the description of the function which uses 3 type parameters:

impl<F, R, C> CreateTexture<TextureContext<F, R, C>> for Texture<R> where
    C: Buffer<R>,
    F: Factory<R>,
    R: Resources, 

In a normal code I write it like this:

                let mut texture_context = window.create_texture_context();
                let texture: pw::G2dTexture = pw::Texture::from_image(

And it works as magic.

Now I want to move this call (creation of a new texture) into a trait for my image (buffer) type. I need to specify type for texture_context . If I write something like that:

pub fn as_texture(self, texture_context: & mut pw::TextureContext) {

It does not work: expected 3 type arguments.

But I have no idea what are the types for texture_context in a normal code (let mut texture_context = window.create_texture_context();). It all done automatically.

I would like to copy type of texture_context into function signature, but I have no idea how to see it in the code. I got used to dynamically typed languages, where I can pry and look, or just print(type(foo)), but Rust does not support this.

Are there any tricks to see the the exact type for an value/variable with inferred type?

You can try telling the compiler that the type is i32 with a type annotation. The error message will tell you the actual type.

1 Like

Yes, I've tried this. Error message does not contain type parameters for a 'good' type. expected i32, found struct piston_window::TextureContext`

... oh, I've missed the 'white' part of the message.

found struct piston_window::TextureContext<gfx_device_gl::factory::Factory, gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>

Yes, thank you.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.