Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ToRouteSegments to borrow self #2283

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ealmloff
Copy link
Member

@ealmloff ealmloff commented Apr 9, 2024

This changes the ToRouteSegments trait to take &self instead of self and adds examples. The router macro currently expects the borrowed version of the type used in the macro to implement ToRouteSegments which can cause confusing errors if you implement it for the type itself.

Currently, you need to implement the trait like this:

#[derive(Clone, Routable, Debug, PartialEq)]
pub(crate) enum Route {
    #[route("/:..search")]
    Bookmarks { search: Segments },
}

#[component]
fn Bookmarks(search: Segments) -> Element {
    todo!()
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct Segments;

impl FromRouteSegments for Segments {
    fn from_route_segments(_: &[&str]) -> Result<Self, Self::Err> {
        todo!()
    }

    type Err = String;
}

impl ToRouteSegments for &Segments {
    fn display_route_segments(self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        todo!()
    }
}

After this PR, you can implement the trait like this:

#[derive(Clone, Routable, Debug, PartialEq)]
pub(crate) enum Route {
    #[route("/:..search")]
    Bookmarks { search: Segments },
}

#[component]
fn Bookmarks(search: Segments) -> Element {
    todo!()
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct Segments;

impl FromRouteSegments for Segments {
    fn from_route_segments(_: &[&str]) -> Result<Self, Self::Err> {
        todo!()
    }

    type Err = String;
}

impl ToRouteSegments for Segments {
    fn display_route_segments(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        todo!()
    }
}

@ealmloff ealmloff added breaking This is a breaking change router Related to the router implementation labels Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This is a breaking change router Related to the router implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant