Skip to content

remove static restriction for str ref#114

Closed
andrei-ng wants to merge 1 commit intoplotly:devfrom
andrei-ng:fix-static-lifetime
Closed

remove static restriction for str ref#114
andrei-ng wants to merge 1 commit intoplotly:devfrom
andrei-ng:fix-static-lifetime

Conversation

@andrei-ng
Copy link
Collaborator

Opening another PR based on the discussion in: #107

@andrei-ng
Copy link
Collaborator Author

@mfreeborn, @aleb , the patch is incorrect because it is missing lifetimes. I have a fix for the above. The function signature should be

pub fn to_inline_html<'a, T: Into<Option<&'a str>>>(&self, plot_div_id: T) -> String {

I build locally and the examples work fine with the above.
Should I fork and make a different PR or is it better we fix it in this already existing PR ?

Hang on a sec, I've just read this properly and I believe what we are looking for is:

pub fn to_inline_html<T: Option<AsRef<str>>(&self, plot_div_id: T) -> String {
    let plot_div_id = plot_div_id.into();
    let plot_div_id = match plot_div_id {
        Some(id) => id.as_ref(),
        None => Alphanumeric.sample_string(&mut thread_rng(), 20),
    };
    self.render_inline(plot_div_id)
}

EDIT: updated what I think should work as an implementation.

@mfreeborn , I tried your suggestion , but it does not work as is. I will try to see if I can get AsRef to work for this case.

@andrei-ng
Copy link
Collaborator Author

This implementation would work

    pub fn to_inline_html<T: AsRef<str>>(&self, plot_div_id: Option<T>) -> String {
        let default = Alphanumeric.sample_string(&mut thread_rng(), 20);
        let plot_div_id = match &plot_div_id {
            Some(id) => id.as_ref(),
            None => default.as_str(),
        }

however the above will not allow one to write

plot.to_inline_html(None)

but the type must be speficied, which I think is ugly

plot.to_inline_html::<String>(None)

Do you @mfreeborn have any suggestion?

@mfreeborn
Copy link
Contributor

Closed by #115 - thanks for your input :)

@mfreeborn mfreeborn closed this Nov 2, 2022
@andrei-ng
Copy link
Collaborator Author

andrei-ng commented Nov 2, 2022

@mfreeborn , thanks! Your solution was indeed simpler :).

@andrei-ng andrei-ng deleted the fix-static-lifetime branch February 5, 2024 12:29
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

Successfully merging this pull request may close these issues.

2 participants