Skip to content

Implement generic bounds translation into Go #222

@AndyTitu

Description

@AndyTitu

📰 Overview

Since PR: #221, typeshare correctly translates struct generics in Go.
The following Rust snippet:

#[typeshare]
pub(crate) struct ItemsGetAllResponse {
    pub(crate) individual_responses: Vec<Response<Item, Error>>,
}

#[typeshare]
pub(crate) struct Response<T, E> {
    pub(crate) content: Option<T>,
    pub(crate) error: Option<E>,
}

Correctly translates to:

type Response[T any, E any] struct {
	Content *T `json:"content,omitempty"`
	Error *E `json:"error,omitempty"`
}

type ItemsGetAllResponse struct {
	IndividualResponses []IndividualResponse[Item, Error] `json:"individualResponses"`
}

However, generic bounds aren't supported:

#[typeshare]
pub(crate) struct IndividualResponse<T: Item, E: Error> {
    pub(crate) content: Option<T>,
    pub(crate) error: Option<E>,
}

doesn't translate to

type Response[T Item, E Error] struct {
	Content *T `json:"content,omitempty"`
	Error *E `json:"error,omitempty"`
}

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