Skip to content

How do I write traits implementation on multiple files? #4

@frederikhors

Description

@frederikhors

Thank you again for this amazing project!

Is there a way to write the below impl on multiple files?

impl<R, M, N> BlogService for Service<R, M, N>
where
    R: BlogRepository,
    M: BlogMetrics,
    N: AuthorNotifier,
{
    async fn create_author(&self, req: &CreateAuthorRequest) -> Result<Author, CreateAuthorError> {
        let result = self.repo.create_author(req).await;
        
        // ...

        result
    }
}

The issue is I have many (hundreds) methods and as you can imagine is difficult to write them on a single file.

If I write in a file

impl<R, M, N> BlogService for Service<R, M, N> {
    async fn create_author(&self, req: &CreateAuthorRequest) -> Result<Author, CreateAuthorError> {}
}

and in another file:

impl<R, M, N> BlogService for Service<R, M, N> {
    async fn delete_author(&self, req: &DeleteAuthorRequest) -> Result<Author, DeleteAuthorError> {}
}

I get from the compiler the error Multiple implementations for the same trait.

I thought it would be a solution to use:

impl<R, M, N> Service<R, M, N> {
    async fn create_author(&self, req: &CreateAuthorRequest) -> Result<Author, CreateAuthorError> {}
    async fn delete_author(&self, req: &DeleteAuthorRequest) -> Result<Author, DeleteAuthorError> {}
}

but this way if you forget to write a trait BlogService's method the compiler doesn't warn you and I don't know other issues with this.

Another way could be to call a method for each of them, like:

impl<R, M, N> BlogService for Service<R, M, N> {
    async fn create_author(&self, req: &CreateAuthorRequest) -> Result<Author, CreateAuthorError> {
        create_author(&self, req: &CreateAuthorRequest)
    }
    async fn delete_author(&self, req: &DeleteAuthorRequest) -> Result<Author, DeleteAuthorError> {
        delete_author(&self, req: &DeleteAuthorRequest)
    }
}

but really?!?!

What do you think?

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