Skip to content

boxed_local doesn't work with the FnBox pattern. #916

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

Closed
Stebalien opened this issue May 11, 2016 · 3 comments
Closed

boxed_local doesn't work with the FnBox pattern. #916

Stebalien opened this issue May 11, 2016 · 3 comments

Comments

@Stebalien
Copy link

In my template library horrorshow, I have the following definition to allow RenderOnce::render_once to be called through a boxed RenderOnce.

pub trait RenderBox {
    /// Do not call. Called by RenderOnce impl on Box<RenderBox>
    #[doc(hidden)]
    fn render_box(self: Box<Self>, tmpl: &mut TemplateBuffer);

    /// Do not call. Called by RenderOnce impl on Box<RenderBox>
    #[doc(hidden)]
    fn size_hint_box(&self) -> usize;
}


impl<T> RenderBox for T where T: RenderOnce {
    fn render_box(self: Box<T>, tmpl: &mut TemplateBuffer) {
        (*self).render_once(tmpl);
    }

    fn size_hint_box(&self) -> usize {
        RenderOnce::size_hint(self)
    }
}

// Box<RenderBox>

impl<'b> RenderOnce for Box<RenderBox + 'b> {
    #[inline]
    fn render_once(self, tmpl: &mut TemplateBuffer) {
        RenderBox::render_box(self, tmpl);
    }

    #[inline]
    fn size_hint(&self) -> usize {
        RenderBox::size_hint_box(self)
    }
}

This is the same pattern as stdlib's FnBox/FnOnce. Unfortunately, clippy claims that this box is unnecessary:

src/render.rs:68:19: 68:23 warning: local variable doesn't need to be boxed here, #[warn(boxed_local)] on by default
src/render.rs:68     fn render_box(self: Box<T>, tmpl: &mut TemplateBuffer) {
@oli-obk
Copy link
Contributor

oli-obk commented May 11, 2016

well... it is "unnecessary" at the implementation site unless you also add a ?Sized bound.

@Stebalien
Copy link
Author

Stebalien commented May 11, 2016

RenderBox must only be implemented on sized T's because it moves a value of type T out of the box (Box<T>). Because RenderBox knows the size of the real type, RenderOnce can be implemented on Box<RenderBox> to recover the size and move the real T out of the box. Unfortunately, I can't give a better explanation because I don't really know how this works under the covers.

See FnBox: https://doc.rust-lang.org/src/alloc/up/src/liballoc/boxed.rs.html#529-533

chrisduerr referenced this issue in chrisduerr/xcbars Jul 8, 2017
All current clippy issues have been fixed. Only one `boxed_local`
warning is left and that is a bug in clippy (see Manishearth/rust-clippy#916).
chrisduerr referenced this issue in chrisduerr/xcbars Jul 8, 2017
All current clippy issues have been fixed. Only one `boxed_local`
warning is left and that is a bug in clippy (see Manishearth/rust-clippy#916).
chrisduerr referenced this issue in chrisduerr/xcbars Jul 8, 2017
All current clippy issues have been fixed. Only one `boxed_local`
warning is left and that is a bug in clippy (see Manishearth/rust-clippy#916).
@phansch
Copy link
Member

phansch commented Oct 16, 2018

Fixed via #3321 (feel free to re-open if it's still a problem)

@phansch phansch closed this as completed Oct 16, 2018
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

3 participants