Skip to main content

value_trait

Attribute Macro value_trait 

Source
#[value_trait]
Expand description

Allows this trait to be used as part of a trait object inside of a value cell, in the form of Vc<Box<dyn MyTrait>>. The annotated trait is made into a subtrait of VcValueTrait.

#[turbo_tasks::value_trait]
pub trait MyTrait {

    #[turbo_tasks::function]
    fn method(self: Vc<Self>, a: i32) -> Vc<Something>;

    // External signature: fn method(self: Vc<Self>, a: i32) -> Vc<Something>
    #[turbo_tasks::function]
    async fn method2(&self, a: i32) -> Result<Vc<Something>> {
        // Default implementation
    }

    // A normal trait item, not a turbo-task
    fn normal(&self) -> SomethingElse;
}

#[turbo_tasks::value_trait]
pub trait OtherTrait: MyTrait + ValueToString {
    // ...
}

#[turbo_tasks::value_impl]
impl MyTrait for MyValue {
    // only the external signature must match (see the docs for #[turbo_tasks::function])
    #[turbo_tasks::function]
    fn method(&self, a: i32) -> Vc<Something> {
        todo!()
    }

    fn normal(&self) -> SomethingElse {
        todo!()
    }
}

The #[turbo_tasks::value_trait] annotation derives VcValueTrait and registers the trait and its methods.

All methods annotated with #[turbo_tasks::function] are cached, and the external signature rewriting rules defined on that macro are applied.

Default implementation are supported.

§Arguments

Example: #[turbo_tasks::value_trait(no_debug, operation)]

§no_debug

Disables the automatic implementation of ValueDebug.

Example: #[turbo_tasks::value_trait(no_debug)]

§Operation

Adds OperationValue as a supertrait of this trait.

Example: #[turbo_tasks::value_trait(operation)]