Skip to content
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

Implement From<Partial::Item> for Partial + Default using apply_some #3

Open
bengreenier opened this issue Sep 17, 2023 · 0 comments
Open
Labels
enhancement New feature or request

Comments

@bengreenier
Copy link
Owner

It should be possible to rework things slightly so that we can go from a Partial::Item into the base struct, when the base struct implements Default.

Example
use partially::Partial;

#[derive(Default)]
struct Base {
    value: String,
}

#[derive(Default)]
struct PartialBase {
    value: Option<String>,
}

impl partially::Partial for Base {
    type Item = PartialBase;

    #[allow(clippy::useless_conversion)]
    fn apply_some(&mut self, partial: Self::Item) {
        if let Some(value) = partial.value {
            self.value = value.into();
        }
    }
}

impl From<PartialBase> for Base {
    fn from(value: PartialBase) -> Self {
        let mut obj = Base::default();

        obj.apply_some(value);

        obj
    }
}

#[test]
fn into_test() {
    let full_partial = PartialBase {
        value: Some("modified".to_string()),
    };

    let base: Base = full_partial.into();
}

@bengreenier bengreenier added the enhancement New feature or request label Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant