Is .boxed() instead of Box::new() a bad idea?

166 points NormalAppearance2851 95 comments

So I started using this on all of my projects

pub trait Boxed: Sized {
    fn boxed(self) -> Box<Self>;
}

impl<T> Boxed for T
where T: Sized
{
    fn boxed(self) -> Box<Self> {
        Box::new(self)
    }
}

So instead of

let box = Box::new(10);

I do

let box = 10.boxed();

And IMO it shines when you do method chaining

let box = Box::new( // 🤓
  value
  .method_1()
  .method_2()
  .method_3()
);

let box =   value // 🗿
  .method_1()
  .method_2()
  .method_3()
  .boxed()

This seems great. But is it? When I got the idea i first searched for crates that do this for you, but I didn't find any. If there aren't any then does this mean that this is a terrible idea?

I think it's great. But I'm only a beginner so I don't know best.