Skip to content

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

@bengreenier

Description

@bengreenier

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions