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

Merged
merged 3 commits into from
Jun 7, 2024

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
@jkelleyrtp jkelleyrtp merged commit a35436e into DioxusLabs:main Jun 7, 2024
9 checks passed
@ealmloff ealmloff deleted the borrow-display-route-segments branch June 7, 2024 19:49
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.

2 participants