Skip to content

Commit

Permalink
Remove a bunch of unnecessary Arcs
Browse files Browse the repository at this point in the history
  • Loading branch information
ihaveint committed Sep 21, 2023
1 parent 62f4a59 commit b5dfba0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
10 changes: 4 additions & 6 deletions chorus_book/src/guide-projector.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ To create a `Projector`, you need to provide the target location and the transpo

```rust
# extern crate chorus_lib;
# use std::sync::Arc;
# use chorus_lib::transport::local::{LocalTransport, LocalTransportChannel};
# use chorus_lib::core::{ChoreographyLocation, Projector};
# use chorus_lib::{LocationSet};
# let transport_channel = Arc::new(LocalTransportChannel::<LocationSet!(Alice, Bob)>::new());
# let alice_transport = LocalTransport::new(Alice, Arc::clone(&transport_channel));
# let transport_channel = LocalTransportChannel::<LocationSet!(Alice, Bob)>::new();
# let alice_transport = LocalTransport::new(Alice, transport_channel.clone());
# #[derive(ChoreographyLocation)]
# struct Alice;
# #[derive(ChoreographyLocation)]
Expand All @@ -31,12 +30,11 @@ To execute a choreography, you need to call the `epp_and_run` method on the `Pro

```rust
# extern crate chorus_lib;
# use std::sync::Arc;
# use chorus_lib::transport::local::{LocalTransport, LocalTransportChannel};
# use chorus_lib::core::{ChoreographyLocation, Projector, Choreography, ChoreoOp};
# use chorus_lib::{LocationSet};
# let transport_channel = Arc::new(LocalTransportChannel::<LocationSet!(Alice, Bob)>::new());
# let alice_transport = LocalTransport::new(Alice, Arc::clone(&transport_channel));
# let transport_channel = LocalTransportChannel::<LocationSet!(Alice, Bob)>::new();
# let alice_transport = LocalTransport::new(Alice, transport_channel.clone());
# #[derive(ChoreographyLocation)]
# struct Alice;
# #[derive(ChoreographyLocation)]
Expand Down
24 changes: 10 additions & 14 deletions chorus_book/src/guide-transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ The `local` transport is used to execute choreographies on the same machine on d

```rust
# extern crate chorus_lib;
# use std::sync::Arc;
# use chorus_lib::core::{ChoreographyLocation};
# use chorus_lib::{LocationSet};
# #[derive(ChoreographyLocation)]
Expand All @@ -21,34 +20,32 @@ The `local` transport is used to execute choreographies on the same machine on d
# struct Bob;
use chorus_lib::transport::local::LocalTransportChannel;

let transport_channel = Arc::new(LocalTransportChannel::<LocationSet!(Alice, Bob)>::new());
let transport_channel = LocalTransportChannel::<LocationSet!(Alice, Bob)>::new();
```

To use the `local` transport, first import the `LocalTransport` struct from the `chorus_lib` crate.

```rust
# extern crate chorus_lib;
# use std::sync::Arc;
# use chorus_lib::core::{ChoreographyLocation};
# use chorus_lib::{LocationSet};
# #[derive(ChoreographyLocation)]
# struct Alice;
# use chorus_lib::transport::local::LocalTransportChannel;
# let transport_channel = Arc::new(LocalTransportChannel::<LocationSet!(Alice)>::new());
# let transport_channel = LocalTransportChannel::<LocationSet!(Alice)>::new();
use chorus_lib::transport::local::{LocalTransport};

let alice_transport = LocalTransport::new(Alice, Arc::clone(&transport_channel));
let alice_transport = LocalTransport::new(Alice, transport_channel.clone());
```

Then build the transport by using the `LocalTransport::new` associated function, which takes a target location (explained in the [Projector section](./guide-projector.md)) and the `LocalTransportChannel`.

You can construct a `LocalTransport` instance by passing a target location(lained in the [Projector section](./guide-projector.md)) and a `std::sync::Arc` of `LocalTransportChannel` to the `new` method.
You can construct a `LocalTransport` instance by passing a target location(lained in the [Projector section](./guide-projector.md)) and a `LocalTransportChannel`.

Because of the nature of the `Local` transport, you must make a `std::sync::Arc` from the `LocalTransportChannel`, by using the `std::sync::Arc::clone(&local_transport)`.
Because of the nature of the `Local` transport, you must use the same `LocalTransportChannel` instance for all locations. You can `clone` the `LocalTransprotChannel` instance and pass it to each `Projector::new` constructor.

```rust
# extern crate chorus_lib;
# use std::sync::Arc;
# use chorus_lib::transport::local::{LocalTransport, LocalTransportChannel};
# use std::thread;
# use chorus_lib::core::{ChoreographyLocation, ChoreoOp, Choreography, Projector};
Expand All @@ -63,19 +60,19 @@ Because of the nature of the `Local` transport, you must make a `std::sync::Arc`
# fn run(self, op: &impl ChoreoOp<Self::L>) {
# }
# }
let transport_channel = Arc::new(LocalTransportChannel::<LocationSet!(Alice, Bob)>::new());
let transport_channel = LocalTransportChannel::<LocationSet!(Alice, Bob)>::new();
let mut handles: Vec<thread::JoinHandle<()>> = Vec::new();
{
// create a transport for Alice
let transport = LocalTransport::new(Alice, Arc::clone(&transport_channel));
let transport = LocalTransport::new(Alice, transport_channel.clone());
handles.push(thread::spawn(move || {
let p = Projector::new(Alice, transport);
p.epp_and_run(HelloWorldChoreography);
}));
}
{
// create another for Bob
let transport = LocalTransport::new(Bob, Arc::clone(&transport_channel));
let transport = LocalTransport::new(Bob, transport_channel.clone());
handles.push(thread::spawn(move || {
let p = Projector::new(Bob, transport);
p.epp_and_run(HelloWorldChoreography);
Expand Down Expand Up @@ -119,7 +116,6 @@ Note that when calling `epp_and_run` on a `Projector`, you will get a compile er

```rust, compile_fail
# extern crate chorus_lib;
# use std::sync::Arc;
# use chorus_lib::transport::local::{LocalTransport, LocalTransportChannel};
# use chorus_lib::core::{ChoreographyLocation, Projector, Choreography, ChoreoOp};
# use chorus_lib::{LocationSet};
Expand All @@ -135,8 +131,8 @@ impl Choreography for HelloWorldChoreography {
}
}
let transport_channel = Arc::new(LocalTransportChannel::<LocationSet!(Alice)>::new());
let transport = LocalTransport::new(Alice, Arc::clone(&transport_channel));
let transport_channel = LocalTransportChannel::<LocationSet!(Alice)>::new();
let transport = LocalTransport::new(Alice, transport_channel.clone());
let projector = Projector::new(Alice, transport);
projector.epp_and_run(HelloWorldChoreography);
```
1 change: 0 additions & 1 deletion chorus_book/src/header.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# extern crate chorus_lib;
# use std::sync::Arc;
# use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation, Projector, Located, Superposition, Runner};
# use chorus_lib::transport::local::{LocalTransport, LocalTransportChannel};
# use chorus_lib::{LocationSet};
Expand Down

0 comments on commit b5dfba0

Please sign in to comment.