Diffstat (limited to 'src/build/builders/owned.rs')
| -rw-r--r-- | src/build/builders/owned.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/build/builders/owned.rs b/src/build/builders/owned.rs new file mode 100644 index 0000000..29098c8 --- /dev/null +++ b/src/build/builders/owned.rs @@ -0,0 +1,56 @@ +use crate::{build::protocols, implementer, protocol::ImplementerExt, walk, Builder}; + +use super::IncompleteValue; + +pub struct OwnedBuilder<T> { + value: Option<T>, +} + +impl<T> Default for OwnedBuilder<T> { + fn default() -> Self { + Self { value: None } + } +} + +impl<'ctx, T: 'static> Builder<'ctx> for OwnedBuilder<T> { + type Error = IncompleteValue; + + type Value = T; + + fn as_visitor(&mut self) -> &mut dyn crate::protocol::Implementer<'ctx> { + self + } + + fn build(self) -> Result<Self::Value, Self::Error> { + self.value.ok_or(IncompleteValue) + } +} + +implementer! { + impl['ctx, T: 'static] OwnedBuilder<T> = [ + protocols::owned::Owned<T>, + protocols::hint::RequestHint, + ]; +} + +impl<'ctx, T: 'static> protocols::hint::RequestHintObject<'ctx> for OwnedBuilder<T> { + fn request_hint( + &mut self, + hints: &mut dyn crate::protocol::Implementer<'ctx>, + ) -> Result<(), ()> { + if let Some(interface) = + hints.interface_for::<walk::protocols::hint::Hint<protocols::owned::Owned<T>>>() + { + interface.as_object().hint(self, ()) + } else { + Ok(()) + } + } +} + +impl<'ctx, T: 'static> protocols::owned::Object<'ctx, T> for OwnedBuilder<T> { + fn visit(&mut self, value: T) -> Result<(), ()> { + self.value = Some(value); + Ok(()) + } +} |