Skip to content

Generate From impl for single field structs #19696

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

Open
A4-Tacks opened this issue Apr 25, 2025 · 0 comments · May be fixed by #19783
Open

Generate From impl for single field structs #19696

A4-Tacks opened this issue Apr 25, 2025 · 0 comments · May be fixed by #19783
Labels
C-feature Category: feature request

Comments

@A4-Tacks
Copy link
Contributor

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))
    }
}
@A4-Tacks A4-Tacks added the C-feature Category: feature request label Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant