From 66ff827c1a8c98f1f3f9a1b13d548c750736c9a4 Mon Sep 17 00:00:00 2001 From: Shun Kashiwa Date: Wed, 13 Nov 2024 16:55:56 -0800 Subject: [PATCH] `cargo fmt` --- chorus_lib/examples/multicast.rs | 4 +- chorus_lib/tests/booksellers.rs | 328 +++++++++++++++++++------------ chorus_lib/tests/kvs.rs | 166 ++++++++-------- 3 files changed, 289 insertions(+), 209 deletions(-) diff --git a/chorus_lib/examples/multicast.rs b/chorus_lib/examples/multicast.rs index a376f7b..4c24913 100644 --- a/chorus_lib/examples/multicast.rs +++ b/chorus_lib/examples/multicast.rs @@ -2,9 +2,7 @@ extern crate chorus_lib; use std::thread; -use chorus_lib::core::{ - ChoreoOp, Choreography, ChoreographyLocation, LocationSet, Projector, -}; +use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation, LocationSet, Projector}; use chorus_lib::transport::local::{LocalTransport, LocalTransportChannelBuilder}; // --- Define two locations (Alice and Bob) --- diff --git a/chorus_lib/tests/booksellers.rs b/chorus_lib/tests/booksellers.rs index 7754b81..c5d3aaa 100644 --- a/chorus_lib/tests/booksellers.rs +++ b/chorus_lib/tests/booksellers.rs @@ -6,14 +6,10 @@ use std::sync::Arc; use std::thread; use chorus_lib::{ - core::{ChoreoOp, Choreography, ChoreographyLocation, Faceted, - FanInChoreography, - HCons, - Here, Located, LocationSet, - LocationSetFoldable, - Member, - Projector, - Quire, Runner, Subset, There}, + core::{ + ChoreoOp, Choreography, ChoreographyLocation, Faceted, FanInChoreography, HCons, Here, + Located, LocationSet, LocationSetFoldable, Member, Projector, Quire, Runner, Subset, There, + }, transport::local::{LocalTransport, LocalTransportChannelBuilder}, }; use chrono::NaiveDate; @@ -32,28 +28,35 @@ type Title = String; type Inventory = HashMap; - trait Decider { type Budgets; fn new(price: Located, Buyer1>, budgets: Self::Budgets) -> Self; } -struct Booksellers> + Decider, Budgets, Buyers, B1Index, BSIndex> { +struct Booksellers< + D: Choreography> + Decider, + Budgets, + Buyers, + B1Index, + BSIndex, +> { inventory: Located, title: Located, budgets: Budgets, _phantoms: PhantomData<(D, Buyers, B1Index, BSIndex)>, } -impl, L = Buyers> + Decider - ,Budgets - ,B1Index - ,BSIndex +impl< + Buyers: LocationSet, + D: Choreography, L = Buyers> + Decider, + Budgets, + B1Index, + BSIndex, > Choreography> for Booksellers - where Buyer1: Member - , Buyer1: Member, There> - , Buyers: Subset, BSIndex> +where + Buyer1: Member, + Buyer1: Member, There>, + Buyers: Subset, BSIndex>, { type L = HCons; fn run(self, op: &impl ChoreoOp) -> Option { @@ -61,7 +64,8 @@ impl; - fn new(price: Located, Buyer1>, budgets: Located) -> Self{ - return Self{price: price, budget: budgets} + fn new(price: Located, Buyer1>, budgets: Located) -> Self { + return Self { + price: price, + budget: budgets, + }; } } impl Choreography> for Unilateral { type L = LocationSet!(Buyer1); fn run(self, op: &impl ChoreoOp) -> Located { - op.locally(Buyer1, |un| { - match un.unwrap(&self.price) { - Some(price) => price <= un.unwrap(&self.budget), - None => false - } + op.locally(Buyer1, |un| match un.unwrap(&self.price) { + Some(price) => price <= un.unwrap(&self.budget), + None => false, }) } } - //////////////////////////////////////////////////////////////////////// struct Colaborative { price: Located, Buyer1>, budgets: Faceted, _phantoms: PhantomData<(B1Index, BSRefl, BSFld)>, } -impl Decider for Colaborative -where Buyer1: Member, - Buyers: Subset +impl Decider + for Colaborative +where + Buyer1: Member, + Buyers: Subset, { type Budgets = Faceted; - fn new(price: Located, Buyer1>, budgets: Faceted) -> Self{ - return Self{price: price, budgets: budgets, _phantoms: PhantomData} + fn new(price: Located, Buyer1>, budgets: Faceted) -> Self { + return Self { + price: price, + budgets: budgets, + _phantoms: PhantomData, + }; } } -impl - Choreography> for Colaborative -where Buyer1: Member, - Buyers: Subset, - Buyers: LocationSetFoldable +impl Choreography> + for Colaborative +where + Buyer1: Member, + Buyers: Subset, + Buyers: LocationSetFoldable, { type L = Buyers; fn run(self, op: &impl ChoreoOp) -> Located { match op.broadcast(Buyer1, self.price) { Some(price) => { - - struct Gather<'a, Buyers: LocationSet, B1Index> { - budgets: &'a Faceted, - _phantoms: PhantomData, - } - impl<'a, Buyers: LocationSet, B1Index> FanInChoreography for Gather<'a, Buyers, B1Index> - where Buyer1: Member + struct Gather<'a, Buyers: LocationSet, B1Index> { + budgets: &'a Faceted, + _phantoms: PhantomData, + } + impl<'a, Buyers: LocationSet, B1Index> FanInChoreography for Gather<'a, Buyers, B1Index> + where + Buyer1: Member, + { + type L = Buyers; + type QS = Buyers; + type RS = LocationSet!(Buyer1); + fn run( + &self, + op: &impl ChoreoOp, + ) -> Located + where + Self::QS: Subset, + Self::RS: Subset, + Q: Member, + Q: Member, { - type L = Buyers; - type QS = Buyers; - type RS = LocationSet!(Buyer1); - fn run( - &self, - op: &impl ChoreoOp, - ) -> Located - where - Self::QS: Subset, - Self::RS: Subset, - Q: Member, - Q: Member, - { - op.comm::<_, Q, Buyer1, Money, (QMemberL, _), QMemberL, B1Index>( - Q::new(), - Buyer1, - &op.locally::(Q::new(), - |un| *un.unwrap3::(&self.budgets)) - ) - } + op.comm::<_, Q, Buyer1, Money, (QMemberL, _), QMemberL, B1Index>( + Q::new(), + Buyer1, + &op.locally::(Q::new(), |un| { + *un.unwrap3::(&self.budgets) + }), + ) } - let budgets = op.fanin( - Buyers::new(), - Gather { - budgets: &self.budgets, - _phantoms: PhantomData, - }, - ); + } + let budgets = op.fanin( + Buyers::new(), + Gather { + budgets: &self.budgets, + _phantoms: PhantomData, + }, + ); - op.locally(Buyer1, |un| { - let budget = un.unwrap::, _, Here>(&budgets).get_map().into_values().sum(); - return price <= budget - }) - }, - None => op.locally(Buyer1, |_| {false}) + op.locally(Buyer1, |un| { + let budget = un + .unwrap::, _, Here>(&budgets) + .get_map() + .into_values() + .sum(); + return price <= budget; + }) + } + None => op.locally(Buyer1, |_| false), } } } - -fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option, answer: Option) { +fn run_test( + inventory: Inventory, + title: Title, + budget1: Money, + budget2: Option, + answer: Option, +) { let transport_channel = LocalTransportChannelBuilder::new() - .with(Seller).with(Buyer1).with(Buyer2) + .with(Seller) + .with(Buyer1) + .with(Buyer2) .build(); let seller_projector = Arc::new(Projector::new( Seller, @@ -215,17 +243,22 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< if let Some(budget2) = budget2 { { let central_runner = Runner::new(); - let choreo : Booksellers, - Faceted, - LocationSet!(Buyer1, Buyer2), - _, _> = Booksellers{ + let choreo: Booksellers< + Colaborative, + Faceted, + LocationSet!(Buyer1, Buyer2), + _, + _, + > = Booksellers { inventory: central_runner.local(inventory.clone()), title: central_runner.local(title.clone()), budgets: central_runner.unsafe_faceted( - [(String::from(Buyer1::name()), budget1), - (String::from(Buyer2::name()), budget2), + [ + (String::from(Buyer1::name()), budget1), + (String::from(Buyer2::name()), budget2), ], - ::new()), + ::new(), + ), _phantoms: PhantomData, }; let central_result = central_runner.run(choreo); @@ -233,10 +266,13 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< } { handles.push(thread::spawn(move || { - let choreo : Booksellers, - Faceted, - LocationSet!(Buyer1, Buyer2), - _, _> = Booksellers{ + let choreo: Booksellers< + Colaborative, + Faceted, + LocationSet!(Buyer1, Buyer2), + _, + _, + > = Booksellers { inventory: seller_projector.local(inventory.clone()), title: seller_projector.remote(Buyer1), budgets: seller_projector.remote_faceted(::new()), @@ -247,13 +283,17 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< } { handles.push(thread::spawn(move || { - let choreo : Booksellers, - Faceted, - LocationSet!(Buyer1, Buyer2), - _, _> = Booksellers{ + let choreo: Booksellers< + Colaborative, + Faceted, + LocationSet!(Buyer1, Buyer2), + _, + _, + > = Booksellers { inventory: buyer1_projector.remote(Seller), title: buyer1_projector.local(title).clone(), - budgets: buyer1_projector.local_faceted(budget1, ::new()), + budgets: buyer1_projector + .local_faceted(budget1, ::new()), _phantoms: PhantomData, }; buyer1_projector.epp_and_run(choreo) @@ -261,13 +301,17 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< } { handles.push(thread::spawn(move || { - let choreo : Booksellers, - Faceted, - LocationSet!(Buyer1, Buyer2), - _, _> = Booksellers{ + let choreo: Booksellers< + Colaborative, + Faceted, + LocationSet!(Buyer1, Buyer2), + _, + _, + > = Booksellers { inventory: buyer2_projector.remote(Seller), title: buyer2_projector.remote(Buyer1), - budgets: buyer2_projector.local_faceted(budget2, ::new()), + budgets: buyer2_projector + .local_faceted(budget2, ::new()), _phantoms: PhantomData, }; buyer2_projector.epp_and_run(choreo) @@ -276,10 +320,13 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< } else { { let central_runner = Runner::new(); - let choreo : Booksellers, - LocationSet!(Buyer1), - _, _> = Booksellers{ + let choreo: Booksellers< + Unilateral, + Located, + LocationSet!(Buyer1), + _, + _, + > = Booksellers { inventory: central_runner.local(inventory.clone()), title: central_runner.local(title.clone()), budgets: central_runner.local(budget1), @@ -290,10 +337,13 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< } { handles.push(thread::spawn(move || { - let choreo : Booksellers, - LocationSet!(Buyer1), - _, _> = Booksellers{ + let choreo: Booksellers< + Unilateral, + Located, + LocationSet!(Buyer1), + _, + _, + > = Booksellers { inventory: seller_projector.local(inventory.clone()), title: seller_projector.remote(Buyer1), budgets: seller_projector.remote(Buyer1), @@ -304,10 +354,13 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< } { handles.push(thread::spawn(move || { - let choreo : Booksellers, - LocationSet!(Buyer1), - _, _> = Booksellers{ + let choreo: Booksellers< + Unilateral, + Located, + LocationSet!(Buyer1), + _, + _, + > = Booksellers { inventory: buyer1_projector.remote(Seller), title: buyer1_projector.local(title).clone(), budgets: buyer1_projector.local(budget1), @@ -318,10 +371,13 @@ fn run_test(inventory: Inventory, title: Title, budget1: Money, budget2: Option< } { handles.push(thread::spawn(move || { - let choreo : Booksellers, - LocationSet!(Buyer1), - _, _> = Booksellers{ + let choreo: Booksellers< + Unilateral, + Located, + LocationSet!(Buyer1), + _, + _, + > = Booksellers { inventory: buyer2_projector.remote(Seller), title: buyer2_projector.remote(Buyer1), budgets: buyer2_projector.remote(Buyer1), @@ -353,10 +409,28 @@ fn main() { }; let tapl = "TAPL".to_string(); let hott = "HoTT".to_string(); - run_test(inventory.clone(), tapl.clone(), 100, None, Some(NaiveDate::from_ymd_opt(2023, 8, 3).unwrap())); + run_test( + inventory.clone(), + tapl.clone(), + 100, + None, + Some(NaiveDate::from_ymd_opt(2023, 8, 3).unwrap()), + ); run_test(inventory.clone(), hott.clone(), 25, None, None); - run_test(inventory.clone(), tapl.clone(), 30, Some(30), Some(NaiveDate::from_ymd_opt(2023, 8, 3).unwrap())); + run_test( + inventory.clone(), + tapl.clone(), + 30, + Some(30), + Some(NaiveDate::from_ymd_opt(2023, 8, 3).unwrap()), + ); run_test(inventory.clone(), hott.clone(), 30, Some(30), None); run_test(inventory.clone(), "nonesuch".to_string(), 25, None, None); - run_test(inventory.clone(), "nonesuch".to_string(), 30, Some(30), None); + run_test( + inventory.clone(), + "nonesuch".to_string(), + 30, + Some(30), + None, + ); } diff --git a/chorus_lib/tests/kvs.rs b/chorus_lib/tests/kvs.rs index 22a13bc..5d68e48 100644 --- a/chorus_lib/tests/kvs.rs +++ b/chorus_lib/tests/kvs.rs @@ -4,20 +4,9 @@ use std::marker::PhantomData; use std::thread; use chorus_lib::core::{ - ChoreoOp, Choreography, ChoreographyLocation, - Deserialize, - Faceted, - FanInChoreography, - HCons, HNil, - Located, LocationSet, - LocationSetFoldable, - Member, - MultiplyLocated, - Projector, - Portable, - Quire, - Serialize, - Subset, + ChoreoOp, Choreography, ChoreographyLocation, Deserialize, Faceted, FanInChoreography, HCons, + HNil, Located, LocationSet, LocationSetFoldable, Member, MultiplyLocated, Portable, Projector, + Quire, Serialize, Subset, }; use chorus_lib::transport::local::{LocalTransport, LocalTransportChannelBuilder}; @@ -38,7 +27,6 @@ fn handle_put(key: Key, val: i32) -> Response { (val != handle_get(key)) as Response } - #[derive(ChoreographyLocation, Debug)] struct Client; @@ -52,30 +40,39 @@ struct Backup1; struct Backup2; // This should perhaps be in core? -struct Gather<'a, - V, - Senders: LocationSet + Subset, - Recievers: LocationSet + Subset, - Census: LocationSet, - SendersPresent, - RecieversPresent> -{ +struct Gather< + 'a, + V, + Senders: LocationSet + Subset, + Recievers: LocationSet + Subset, + Census: LocationSet, + SendersPresent, + RecieversPresent, +> { values: &'a Faceted, phantom: PhantomData<(Census, SendersPresent, Recievers, RecieversPresent)>, } -impl<'a, - V: Portable + Copy, - Senders: LocationSet + Subset, - Recievers: LocationSet + Subset, - Census: LocationSet, - SendersPresent, - RecieversPresent> - FanInChoreography for Gather<'a, V, Senders, Recievers, Census, SendersPresent, RecieversPresent> +impl< + 'a, + V: Portable + Copy, + Senders: LocationSet + Subset, + Recievers: LocationSet + Subset, + Census: LocationSet, + SendersPresent, + RecieversPresent, + > FanInChoreography + for Gather<'a, V, Senders, Recievers, Census, SendersPresent, RecieversPresent> { type L = Census; type QS = Senders; type RS = Recievers; - fn run( + fn run< + Sender: ChoreographyLocation, + _SendersPresent, + _RecieversPresent, + SenderPresent, + SenderInSenders, + >( &self, op: &impl ChoreoOp, ) -> MultiplyLocated @@ -95,61 +92,67 @@ impl<'a, } } - -struct HandleRequest -{ +struct HandleRequest { request: Located, _phantoms: PhantomData<(Backups, BackupsPresent, BSpine)>, } -impl - Choreography> for HandleRequest -where Backups: Subset, BackupsPresent>, - Backups: LocationSetFoldable, Backups, BSpine> +impl Choreography> + for HandleRequest +where + Backups: Subset, BackupsPresent>, + Backups: LocationSetFoldable, Backups, BSpine>, { type L = HCons; fn run(self, op: &impl ChoreoOp) -> Located { match op.broadcast(Server, self.request) { Request::Put(key, value) => { - let oks = op.parallel(Backups::new(), ||{ handle_put(key.clone(), value) }); + let oks = op.parallel(Backups::new(), || handle_put(key.clone(), value)); let gathered = op.fanin::, _, _, _, _>( Backups::new(), - Gather{values: &oks, phantom: PhantomData} - ); + Gather { + values: &oks, + phantom: PhantomData, + }, + ); op.locally(Server, |un| { - let ok = un.unwrap::, _, _>(&gathered).get_map().into_values().all(|response|{response == 0}); - if ok { - return handle_put(key.clone(), value) - } else { - return -1 - } + let ok = un + .unwrap::, _, _>(&gathered) + .get_map() + .into_values() + .all(|response| response == 0); + if ok { + return handle_put(key.clone(), value); + } else { + return -1; + } }) } - Request::Get(key) => op.locally(Server, |_| { handle_get(key.clone()) }) + Request::Get(key) => op.locally(Server, |_| handle_get(key.clone())), } } } -struct KVS -{ +struct KVS { request: Located, _phantoms: PhantomData<(Backups, BackupsPresent, BackupsAreServers, BSpine)>, } impl - Choreography> for KVS -where Backups: Subset>, BackupsPresent>, - Backups: Subset, BackupsAreServers>, - Backups: LocationSetFoldable, Backups, BSpine> + Choreography> + for KVS +where + Backups: Subset>, BackupsPresent>, + Backups: Subset, BackupsAreServers>, + Backups: LocationSetFoldable, Backups, BSpine>, { type L = HCons>; fn run(self, op: &impl ChoreoOp) -> Located { let request = op.comm(Client, Server, &self.request); - let response = op.enclave(HandleRequest::{ - request: request, - _phantoms: PhantomData - }).flatten(); + let response = op + .enclave(HandleRequest:: { + request: request, + _phantoms: PhantomData, + }) + .flatten(); op.comm(Server, Client, &response) } } @@ -161,13 +164,13 @@ fn run_test(request: Request, answer: Response) { .with(Backup1) .with(Backup2) .build(); - let transport_client = LocalTransport::new(Client, transport_channel.clone()); - let transport_server = LocalTransport::new(Server, transport_channel.clone()); + let transport_client = LocalTransport::new(Client, transport_channel.clone()); + let transport_server = LocalTransport::new(Server, transport_channel.clone()); let transport_backup1 = LocalTransport::new(Backup1, transport_channel.clone()); let transport_backup2 = LocalTransport::new(Backup2, transport_channel.clone()); - let client_projector = Projector::new(Client, transport_client); - let server_projector = Projector::new(Server, transport_server); + let client_projector = Projector::new(Client, transport_client); + let server_projector = Projector::new(Server, transport_server); let backup1_projector = Projector::new(Backup1, transport_backup1); let backup2_projector = Projector::new(Backup2, transport_backup2); @@ -176,7 +179,7 @@ fn run_test(request: Request, answer: Response) { thread::Builder::new() .name("Server".to_string()) .spawn(move || { - server_projector.epp_and_run(KVS::>, _, _, _>{ + server_projector.epp_and_run(KVS::>, _, _, _> { request: server_projector.remote(Client), _phantoms: PhantomData, }) @@ -187,10 +190,12 @@ fn run_test(request: Request, answer: Response) { thread::Builder::new() .name("Backup1".to_string()) .spawn(move || { - backup1_projector.epp_and_run(KVS::>, _, _, _>{ - request: backup1_projector.remote(Client), - _phantoms: PhantomData, - }) + backup1_projector.epp_and_run( + KVS::>, _, _, _> { + request: backup1_projector.remote(Client), + _phantoms: PhantomData, + }, + ) }) .unwrap(), ); @@ -198,17 +203,20 @@ fn run_test(request: Request, answer: Response) { thread::Builder::new() .name("Backup2".to_string()) .spawn(move || { - backup2_projector.epp_and_run(KVS::>, _, _, _>{ - request: backup2_projector.remote(Client), - _phantoms: PhantomData, - }) + backup2_projector.epp_and_run( + KVS::>, _, _, _> { + request: backup2_projector.remote(Client), + _phantoms: PhantomData, + }, + ) }) .unwrap(), ); - let retval = client_projector.epp_and_run(KVS::>, _, _, _>{ - request: client_projector.local(request), - _phantoms: PhantomData, - }); + let retval = + client_projector.epp_and_run(KVS::>, _, _, _> { + request: client_projector.local(request), + _phantoms: PhantomData, + }); for handle in handles { handle.join().unwrap(); }