Skip to content

Generate From impl for single field structs #19696

Open
@A4-Tacks

Description

@A4-Tacks

Similar CodeAction Generate `From` impl for this enum variant

struct Foo(String);
struct Bar {
    inner: String,
}
// repr(transparent) bound
struct Baz {
    inner: String,
    _phantom: core::marker::PhantomData<()>,
    _pinned: core::marker::PhantomPinned,
    _zero_size_align_1: (),
}

Generate:

impl From<String> for Foo {
    fn from(value: String) -> Self {
        Self(value)
    }
}
impl From<String> for Bar {
    fn from(inner: String) -> Self {
        Self { inner }
    }
}
impl From<String> for Baz {
    fn from(inner: String) -> Self {
        Self {
            inner,
            _phantom: core::marker::PhantomData,
            _pinned: core::marker::PhantomPinned,
            _zero_size_align_1: (),
        }
    }
}

Consider going further:

struct Pack(Option<String>);

Generate:

impl From<String> for Pack {
    fn from(value: String) -> Self {
        Self(Some(value))
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-featureCategory: feature request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions