Skip to main content

cgp_field_extra/impls/
set_optional.rs

1use core::marker::PhantomData;
2
3use cgp_field::impls::IsOptional;
4use cgp_field::traits::UpdateField;
5
6pub trait SetOptional<Tag> {
7    type Value;
8
9    fn set(self, _tag: PhantomData<Tag>, value: Self::Value) -> Self;
10
11    fn set_optional(
12        self,
13        _tag: PhantomData<Tag>,
14        value: Self::Value,
15    ) -> (Option<Self::Value>, Self);
16}
17
18impl<Context, Tag> SetOptional<Tag> for Context
19where
20    Context: UpdateField<Tag, IsOptional, Mapper = IsOptional, Output = Context>,
21{
22    type Value = Context::Value;
23
24    fn set(self, tag: PhantomData<Tag>, value: Self::Value) -> Self {
25        self.set_optional(tag, value).1
26    }
27
28    fn set_optional(
29        self,
30        tag: PhantomData<Tag>,
31        value: Self::Value,
32    ) -> (Option<Self::Value>, Self) {
33        self.update_field(tag, Some(value))
34    }
35}