Skip to content

Pin<Box<impl Default>> should impl Default #143688

@Nemo157

Description

@Nemo157

I tried this code (playground):

use std::pin::Pin;

#[derive(Default)]
pub struct Foo {
    pub bar: Pin<Box<i32>>,
}

I expected to see this happen: it compile.

Instead, this happened:

error[E0277]: the trait bound `Pin<Box<i32>>: Default` is not satisfied
 --> src/lib.rs:5:5
  |
3 | #[derive(Default)]
  |          ------- in this derive macro expansion
4 | pub struct Foo {
5 |     pub bar: Pin<Box<i32>>,
  |     ^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `Pin<Box<i32>>`

I believe this impl should have been provided:

impl<T: Default> Default for Pin<Box<T>>

Unfortunately because Box is #[fundamental] I believe this can't be added now, this code is currently accepted and would have an overlapping implementation (playground):

use std::pin::Pin;

pub struct Bar;

impl Default for Bar {
    fn default() -> Self {
        Self
    }
}

impl Default for Pin<Box<Bar>> {
    fn default() -> Self {
        Box::pin(Bar)
    }
}

#[derive(Default)]
pub struct Foo {
    pub bar: Pin<Box<Bar>>,
}

Unless potentially something to do with specialization would allow this overlap in the future? Also, AFAICT nobody else has cared enough about this to open an issue in the 6 years Pin has been stable, so maybe it can just be closed as an unfortunate API limitation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions