harmos_domain/operations/common/
direction.rs1use std::any::TypeId;
21use std::marker::PhantomData;
22
23use crate::schema::Described;
27use crate::schema::declaration::Definition;
28use crate::schema::graph::Field;
29use crate::{MemorySize, MessageError};
30
31pub struct Stream<T>(PhantomData<fn() -> T>);
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum NoStream {}
45
46pub trait Direction: Send + Sync + 'static {
49 type Item: crate::Portable;
50 const STREAMED: bool;
51
52 fn schema() -> Option<Definition> {
53 Self::STREAMED.then_some(<Self::Item as Described>::DEFINITION)
54 }
55
56 fn item_type() -> Option<TypeId> {
57 Self::STREAMED.then(TypeId::of::<Self::Item>)
58 }
59}
60
61impl<T: crate::Portable> Direction for Stream<T> {
64 type Item = T;
65 const STREAMED: bool = true;
66}
67
68impl Direction for NoStream {
69 type Item = NoStream;
70 const STREAMED: bool = false;
71}
72
73impl Described for NoStream {
74 const NAME: &'static str = "harmos::no_stream";
75 const FIELDS: &'static [Field<Definition>] = &[];
76 const MARKER: &'static [u8] = &crate::schema::declaration::encode::<
77 { crate::schema::declaration::encoded_len(Self::NAME, Self::FIELDS) },
78 >(Self::NAME, Self::FIELDS);
79}
80
81impl MemorySize for NoStream {
82 fn memory_size(&self) -> usize {
83 match *self {}
84 }
85}
86
87impl crate::__private::PostcardValue for NoStream {
88 fn write(&self, _: &mut Vec<u8>) {
89 match *self {}
90 }
91 fn size(&self) -> usize {
92 match *self {}
93 }
94 fn memory(_: &mut crate::__private::PostcardReader<'_>) -> Result<usize, MessageError> {
95 Err(absent())
96 }
97 fn read(_: &mut crate::__private::PostcardReader<'_>) -> Result<Self, MessageError> {
98 Err(absent())
99 }
100}
101
102pub fn item_types<O: super::OperationDeclaration>() -> (Option<TypeId>, Option<TypeId>) {
106 (<O::Input as Direction>::item_type(), <O::Output as Direction>::item_type())
107}
108
109fn absent() -> MessageError {
112 MessageError::borrowed("the operation streams no items in this direction")
113}
114
115