Import type alias with use


I’m using the crate gfx-hal and it defines some type aliases like this:

pub trait Backend: 'static + Sized + Eq + Clone + Hash + Debug + Any + Send + Sync {
    type Fence: Debug + Any + Send + Sync;
    // more definitions ....

now I want to use those aliases in my crate but how do I import those?
I tried it like this:

use gfx_hal::Backend::Fence;

but it says:

‘Fence’ is not directly importable

This is a trait definition with an associated type, so Fence is not usable unless you take it from a type which implemented Backend. For example: gfx_backend_vulkan::Backend.

1 Like

When I try to use it as Vec type param it says:

no variant ‘Fence’ on enum gfx_backend_vulkan::Backend

Is the Backend trait in scope?

Yes it is:

use hal::{
    Instance, Surface, PhysicalDevice, Device, Swapchain, SwapchainConfig,
    Graphics, Backend,

    format::{ChannelType, Format, Swizzle, Aspects,},
        Attachment, AttachmentOps, AttachmentLoadOp, AttachmentStoreOp,
    image::{Layout, ViewKind, SubresourceRange,},

pub struct Hal {
    in_flight_fences: Vec<backend::Backend::Fence,

And backend::Backend is a type that implements the hal::Backend trait?

Well, gfx_backend_vulkan implements gfx_hal::Backend, but it implements gfx_hal-v0.2.1::Backend, so are you sure that you are also using the right versions for everything? gfx-backend-Vulkan-v0.2.3 is the one referred to in the docs I linked.

i have this

version = "0.2"
optional = true

in my Cargo.toml. So it should pick 0.2.3 am I right?

What about gfx-hal's version?

Also, please try the following:

let fences: Vec<<gfx_backend_vulkan::Backend as gfx_hal::Backend>::Fence>;

Oh this works, thank you!

1 Like