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

Add into_content(self) -> D method for StyledContent<D> #932

Open
Holzhaus opened this issue Sep 25, 2024 · 0 comments
Open

Add into_content(self) -> D method for StyledContent<D> #932

Holzhaus opened this issue Sep 25, 2024 · 0 comments

Comments

@Holzhaus
Copy link

Holzhaus commented Sep 25, 2024

Is your feature request related to a problem? Please describe.
I have multiple StyledContent<D> object with different D. I want to convert them all to StyledContent<Cow<'a, str>>. Since content is a private field and content(&self) -> &D returns a reference, i cannot move the content out of the struct in order to convert them.

My current workaround involves cloning the value:

fn convert<'a, D: Clone + fmt::Display + Into<Cow<'a, str>>>(value: StyledContent<D>) -> StyledContent<Cow<'a, str>> {
    let style = *value.style();
    let content: D = value.content().clone();
    StyledContent::new(style, content.into())
}

Describe the solution you'd like
Add an into_content(self) -> D method that moves the content out of the struct. This would allow implementing something like this:

fn convert<'a, D: fmt::Display + Into<Cow<'a, str>>>(value: StyledContent<D>) -> StyledContent<Cow<'a, str>> {
    let style = *value.style();
    let content: D = value.into_content();
    StyledContent::new(style, content.into())
}

Describe alternatives you've considered if any

  • The workaround with Clone works, but involves cloning the value unnecessarily and introduced an additional trait bound.
  • Destructuring is not possible, since the fields are private.
  • Of course I could also just call to_string() on the StyledContent and use these instead (and convert them into a StyledContent<Cow<'a, str>>). However, this seems hacky and I still need access to the unstyled content later, so this is not an option (because then I would need to strip the style later.

Additional context
n/a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant