hyperactor_mesh/resource/
mesh.rs1#![allow(dead_code)]
10
11use hyperactor::Named;
12use ndslice::Extent;
19use serde::Deserialize;
20use serde::Serialize;
21
22use crate::resource::Resource;
23use crate::resource::Status;
24use crate::v1::ValueMesh;
25
26#[derive(Debug, Named, Serialize, Deserialize)]
28pub struct Spec<S> {
29 extent: Extent,
31 spec: S,
34}
35
36#[derive(Debug, Named, Serialize, Deserialize)]
38pub struct State<S> {
39 statuses: ValueMesh<Status>,
41 state: S,
43}
44
45pub trait Mesh {
47 type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
49
50 type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
52}
53
54impl<M: Mesh> Resource for M {
55 type Spec = Spec<M::Spec>;
56 type State = State<M::State>;
57}
58
59#[cfg(test)]
60mod test {
61 use hyperactor::Actor;
62 use hyperactor::Context;
63 use hyperactor::Handler;
64
65 use super::*;
66 use crate::resource::Controller;
67 use crate::resource::CreateOrUpdate;
68 use crate::resource::GetState;
69 use crate::resource::Stop;
70
71 macro_rules! handler {
74 (
75 $actor:path,
76 $(
77 $name:ident: $msg:ty => $body:expr
78 ),* $(,)?
79 ) => {
80 $(
81 #[async_trait::async_trait]
82 impl Handler<$msg> for $actor {
83 async fn handle(
84 &mut self,
85 #[allow(unused_variables)]
86 cx: & Context<Self>,
87 $name: $msg
88 ) -> anyhow::Result<()> {
89 $body
90 }
91 }
92 )*
93 };
94 }
95
96 #[derive(Debug, Named, Serialize, Deserialize)]
97 struct TestMesh;
98
99 impl Mesh for TestMesh {
100 type Spec = ();
101 type State = ();
102 }
103
104 #[derive(Actor, Debug, Default, Named, Serialize, Deserialize)]
105 struct TestMeshController;
106
107 handler! {
109 TestMeshController,
110 _message: CreateOrUpdate<Spec<()>> => unimplemented!(),
111 _message: GetState<State<()>> => unimplemented!(),
112 _message: Stop => unimplemented!(),
113 }
114
115 hyperactor::assert_behaves!(TestMeshController as Controller<TestMesh>);
116}