Skip to content

[type safety] add type enforcement for SubscriptionSink? #1582

@devtempmac

Description

@devtempmac

Current rpc definition allows adding types to item:

#[subscription(name = "subscribeStorage" => "override", item = Vec<Hash>)]
    async fn subscribe_storage(&self, keys: Option<Vec<StorageKey>>) -> SubscriptionResult;

but this isn't enforced at the server-side:

async fn subscribe_storage(
        &self,
        pending: PendingSubscriptionSink,
        _keys: Option<Vec<ExampleStorageKey>>,
    ) -> SubscriptionResult {
        let sink = pending.accept().await?;
        let json = serde_json::value::to_raw_value(&vec![[0.123123; 32]])?; // compiles ok (can send anything)
        sink.send(json).await?;

        Ok(())
    }

Can this be enforced with SubscriptionSink by adding a generic param?

On the serverimplementation side, it would be:

async fn subscribe_storage(
        &self,
        pending: PendingSubscriptionSink<Hash>,
        _keys: Option<Vec<ExampleStorageKey>>,
    ) -> SubscriptionResult {
        let sink = pending.accept().await?;
        // ...
        sink.send(not_hash).await?; // <-- compiler type error
        sink.send(some_hash).await?; // <-- compiler ok

        Ok(())
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions