From 0de1a302fb204886f43d5107bb65f00c87119344 Mon Sep 17 00:00:00 2001 From: Shun Kashiwa Date: Tue, 3 Oct 2023 00:11:11 -0700 Subject: [PATCH] rename HList into `LocationSet` --- chorus_lib/src/core.rs | 62 +++++++++++++++++-------------- chorus_lib/src/transport.rs | 8 ++-- chorus_lib/src/transport/http.rs | 10 ++--- chorus_lib/src/transport/local.rs | 18 ++++----- 4 files changed, 53 insertions(+), 45 deletions(-) diff --git a/chorus_lib/src/core.rs b/chorus_lib/src/core.rs index 426ad39..5fe92ab 100644 --- a/chorus_lib/src/core.rs +++ b/chorus_lib/src/core.rs @@ -106,7 +106,7 @@ where /// heterogeneous list #[doc(hidden)] -pub trait HList { +pub trait LocationSet { /// returns fn to_string_list() -> Vec<&'static str>; } @@ -119,15 +119,15 @@ pub struct HNil; #[doc(hidden)] pub struct HCons(Head, Tail); -impl HList for HNil { +impl LocationSet for HNil { fn to_string_list() -> Vec<&'static str> { Vec::new() } } -impl HList for HCons +impl LocationSet for HCons where Head: ChoreographyLocation, - Tail: HList, + Tail: LocationSet, { fn to_string_list() -> Vec<&'static str> { let mut v = Tail::to_string_list(); @@ -180,12 +180,12 @@ pub struct There(Index); /// If a location `L1` is in `L`, then there exists a type `Index` such that `L1` implements `Member`. pub trait Member { /// A location set that is the remainder of `L` after removing the member. - type Remainder: HList; + type Remainder: LocationSet; } impl Member, Here> for Head where - Tail: HList, + Tail: LocationSet, { type Remainder = Tail; } @@ -204,13 +204,14 @@ where /// /// It takes two type parameters `L` and `Index`. `L` is a location set and `Index` is some type that is inferred by the compiler. /// If a location set `M` is a subset of `L`, then there exists a type `Index` such that `M` implements `Subset`. -pub trait Subset {} +pub trait Subset {} // Base case: HNil is a subset of any set -impl Subset for HNil {} +impl Subset for HNil {} // Recursive case -impl Subset> for HCons +impl Subset> + for HCons where Head: Member, Tail: Subset, @@ -233,7 +234,7 @@ impl Unwrapper { /// /// The trait provides methods to work with located values. An implementation of the trait is "injected" into /// a choreography at runtime and provides the actual implementation of the operators. -pub trait ChoreoOp { +pub trait ChoreoOp { /// Performs a computation at the specified location. /// /// `locally` performs a computation at a location, which are specified by `location` and `computation`, respectively. @@ -278,10 +279,10 @@ pub trait ChoreoOp { /// Calls a choreography. fn call>(&self, choreo: C) -> R where - M: HList + Subset; + M: LocationSet + Subset; /// Calls a choreography on a subset of locations. - fn colocally, Index>( + fn colocally, Index>( &self, choreo: C, ) -> R @@ -298,7 +299,7 @@ pub trait ChoreoOp { /// The trait provides a method `run` that takes an implementation of `ChoreoOp` and returns a value of type `R`. pub trait Choreography { /// Locations - type L: HList; + type L: LocationSet; /// A method that executes a choreography. /// /// The method takes an implementation of `ChoreoOp`. Inside the method, you can use the operators provided by `ChoreoOp` to define a choreography. @@ -314,7 +315,7 @@ pub trait Choreography { /// The type parameter `L` is the location set that the transport is operating on. /// /// The type parameter `TargetLocation` is the target `ChoreographyLocation`. -pub trait Transport { +pub trait Transport { /// Returns a list of locations. fn locations(&self) -> Vec; /// Sends a message from `from` to `to`. @@ -324,7 +325,7 @@ pub trait Transport { } /// Provides a method to perform end-point projection. -pub struct Projector, Index> +pub struct Projector, Index> where L1: Member, { @@ -334,7 +335,8 @@ where index: PhantomData, } -impl, Index> Projector +impl, Index> + Projector where L1: Member, { @@ -377,22 +379,28 @@ where } /// Performs end-point projection and runs a choreography. - pub fn epp_and_run<'a, V, L: HList, C: Choreography, IndexSet>( + pub fn epp_and_run<'a, V, L: LocationSet, C: Choreography, IndexSet>( &'a self, choreo: C, ) -> V where L: Subset, { - struct EppOp<'a, L: HList, L1: ChoreographyLocation, LS: HList, B: Transport> { + struct EppOp< + 'a, + L: LocationSet, + L1: ChoreographyLocation, + LS: LocationSet, + B: Transport, + > { target: PhantomData, transport: &'a B, locations: Vec, marker: PhantomData, projector_location_set: PhantomData, } - impl<'a, L: HList, T: ChoreographyLocation, LS: HList, B: Transport> ChoreoOp - for EppOp<'a, L, T, LS, B> + impl<'a, L: LocationSet, T: ChoreographyLocation, LS: LocationSet, B: Transport> + ChoreoOp for EppOp<'a, L, T, LS, B> { fn locally( &self, @@ -453,7 +461,7 @@ where fn call>(&self, choreo: C) -> R where - M: HList + Subset, + M: LocationSet + Subset, { let op: EppOp<'a, M, T, LS, B> = EppOp { target: PhantomData::, @@ -465,7 +473,7 @@ where choreo.run(&op) } - fn colocally, Index>( + fn colocally, Index>( &self, choreo: C, ) -> R { @@ -499,11 +507,11 @@ where } /// Provides a method to run a choreography without end-point projection. -pub struct Runner { +pub struct Runner { marker: PhantomData, } -impl Runner { +impl Runner { /// Constructs a runner. pub fn new() -> Self { Runner { @@ -528,7 +536,7 @@ impl Runner { /// Runs a choreography directly pub fn run<'a, V, C: Choreography>(&'a self, choreo: C) -> V { struct RunOp(PhantomData); - impl ChoreoOp for RunOp { + impl ChoreoOp for RunOp { fn locally( &self, _location: L1, @@ -569,13 +577,13 @@ impl Runner { fn call>(&self, choreo: C) -> R where - M: HList + Subset, + M: LocationSet + Subset, { let op: RunOp = RunOp(PhantomData); choreo.run(&op) } - fn colocally, Index>( + fn colocally, Index>( &self, choreo: C, ) -> R { diff --git a/chorus_lib/src/transport.rs b/chorus_lib/src/transport.rs index 1ddfc70..418e45f 100644 --- a/chorus_lib/src/transport.rs +++ b/chorus_lib/src/transport.rs @@ -3,13 +3,13 @@ pub mod http; pub mod local; -use crate::core::{ChoreographyLocation, HCons, HList, LocationSet}; +use crate::core::{ChoreographyLocation, HCons, LocationSet}; use std::collections::HashMap; use std::marker::PhantomData; /// A generic struct for configuration of `Transport`. #[derive(Clone)] -pub struct TransportConfig { +pub struct TransportConfig { /// The information about locations pub info: HashMap, /// The information about the target choreography @@ -38,7 +38,7 @@ pub struct TransportConfig { +pub struct TransportConfigBuilder { target: (Target, TargetInfo), location_set: PhantomData, info: HashMap, @@ -57,7 +57,7 @@ impl } } -impl +impl TransportConfigBuilder { /// Adds information about a new `ChoreographyLocation`. diff --git a/chorus_lib/src/transport/http.rs b/chorus_lib/src/transport/http.rs index bb019ea..4e6024d 100644 --- a/chorus_lib/src/transport/http.rs +++ b/chorus_lib/src/transport/http.rs @@ -13,7 +13,7 @@ use tiny_http::Server; use ureq::{Agent, AgentBuilder}; use crate::{ - core::{ChoreographyLocation, HList, Member, Portable, Transport}, + core::{ChoreographyLocation, LocationSet, Member, Portable, Transport}, transport::{TransportConfig, TransportConfigBuilder}, utils::queue::BlockingQueue, }; @@ -48,7 +48,7 @@ pub type HttpTransportConfigBuilder = const HEADER_SRC: &str = "X-CHORUS-SOURCE"; /// The HTTP transport. -pub struct HttpTransport { +pub struct HttpTransport { config: HashMap, agent: Agent, server: Arc, @@ -58,7 +58,7 @@ pub struct HttpTransport { target_location: PhantomData, } -impl HttpTransport { +impl HttpTransport { /// Creates a new `HttpTransport` instance from the configuration. pub fn new(http_config: HttpTransportConfig) -> Self where @@ -119,14 +119,14 @@ impl HttpTransport { } } -impl Drop for HttpTransport { +impl Drop for HttpTransport { fn drop(&mut self) { self.server.unblock(); self.join_handle.take().map(thread::JoinHandle::join); } } -impl Transport +impl Transport for HttpTransport { fn locations(&self) -> Vec { diff --git a/chorus_lib/src/transport/local.rs b/chorus_lib/src/transport/local.rs index 9e7f447..9419f53 100644 --- a/chorus_lib/src/transport/local.rs +++ b/chorus_lib/src/transport/local.rs @@ -7,19 +7,19 @@ use serde_json; use std::marker::PhantomData; -use crate::core::{ChoreographyLocation, HCons, HList, LocationSet, Portable, Transport}; +use crate::core::{ChoreographyLocation, HCons, LocationSet, Portable, Transport}; use crate::utils::queue::BlockingQueue; type QueueMap = HashMap>>; /// A Transport channel used between multiple `Transport`s. -pub struct LocalTransportChannel { +pub struct LocalTransportChannel { /// The location set where the channel is defined on. location_set: std::marker::PhantomData, queue_map: Arc, } -impl Clone for LocalTransportChannel { +impl Clone for LocalTransportChannel { fn clone(&self) -> Self { LocalTransportChannel { location_set: PhantomData, @@ -28,7 +28,7 @@ impl Clone for LocalTransportChannel { } } -impl LocalTransportChannel { +impl LocalTransportChannel { /// Creates a new `LocalTransportChannel` instance. /// /// You must specify the location set of the channel. The channel can only be used by locations in the set. @@ -87,7 +87,7 @@ impl LocalTransportChannel { /// .with(Bob) /// .build(); /// ``` -pub struct LocalTransportChannelBuilder { +pub struct LocalTransportChannelBuilder { location_set: PhantomData, } @@ -100,7 +100,7 @@ impl LocalTransportChannelBuilder { } } -impl LocalTransportChannelBuilder { +impl LocalTransportChannelBuilder { /// Adds a new location to the set of locations in the `LocalTransportChannel`. pub fn with( &self, @@ -123,14 +123,14 @@ impl LocalTransportChannelBuilder { /// This transport uses a blocking queue to allow for communication between threads. Each location must be executed in its thread. /// /// All locations must share the same `LocalTransportChannel` instance. `LocalTransportChannel` implements `Clone` so that it can be shared across threads. -pub struct LocalTransport { +pub struct LocalTransport { internal_locations: Vec, location_set: PhantomData, local_channel: LocalTransportChannel, target_location: PhantomData, } -impl LocalTransport { +impl LocalTransport { /// Creates a new `LocalTransport` instance from a Target `ChoreographyLocation` and a `LocalTransportChannel`. pub fn new(target: TargetLocation, local_channel: LocalTransportChannel) -> Self { _ = target; @@ -151,7 +151,7 @@ impl LocalTransport { } } -impl Transport +impl Transport for LocalTransport { fn locations(&self) -> Vec {