Skip to content

Commit

Permalink
Remove mutability requirement from Lighthouse
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Jun 1, 2024
1 parent 51c9bb3 commit 18eca5b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lighthouse-client/examples/admin_crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use lighthouse_client::{protocol::Authentication, Error, Lighthouse, Result, TokioWebSocket, LIGHTHOUSE_URL};
use tracing::{info, info_span, Instrument};

async fn run(mut lh: Lighthouse<TokioWebSocket>) -> Result<()> {
async fn run(lh: Lighthouse<TokioWebSocket>) -> Result<()> {
info!("Connected to the Lighthouse server");

async {
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-client/examples/admin_get_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use lighthouse_client::{protocol::Authentication, Lighthouse, Result, TokioWebSocket, LIGHTHOUSE_URL};
use tracing::info;

async fn run(mut lh: Lighthouse<TokioWebSocket>) -> Result<()> {
async fn run(lh: Lighthouse<TokioWebSocket>) -> Result<()> {
info!("Connected to the Lighthouse server");

let metrics = lh.get_laser_metrics().await?.payload;
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-client/examples/admin_list_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use lighthouse_client::{protocol::Authentication, Lighthouse, Result, TokioWebSocket, LIGHTHOUSE_URL};
use tracing::info;

async fn run(mut lh: Lighthouse<TokioWebSocket>) -> Result<()> {
async fn run(lh: Lighthouse<TokioWebSocket>) -> Result<()> {
info!("Connected to the Lighthouse server");

let tree = lh.list(&[]).await?.payload;
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-client/examples/black.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use lighthouse_client::{protocol::{Authentication, Color, Frame}, Lighthouse, Result, TokioWebSocket, LIGHTHOUSE_URL};
use tracing::info;

async fn run(mut lh: Lighthouse<TokioWebSocket>) -> Result<()> {
async fn run(lh: Lighthouse<TokioWebSocket>) -> Result<()> {
info!("Connected to the Lighthouse server");

lh.put_model(Frame::fill(Color::BLACK)).await?;
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-client/examples/disco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tracing::info;
use tokio::time;
use std::time::Duration;

async fn run(mut lh: Lighthouse<TokioWebSocket>) -> Result<()> {
async fn run(lh: Lighthouse<TokioWebSocket>) -> Result<()> {
info!("Connected to the Lighthouse server");

loop {
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-client/examples/input_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lighthouse_client::{protocol::Authentication, Lighthouse, Result, TokioWebSo
use lighthouse_protocol::Model;
use tracing::info;

async fn run(mut lh: Lighthouse<TokioWebSocket>) -> Result<()> {
async fn run(lh: Lighthouse<TokioWebSocket>) -> Result<()> {
info!("Connected to the Lighthouse server");

// Stream input events
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-client/examples/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl State {
}
}

async fn run_updater(mut lh: Lighthouse<TokioWebSocket>, shared_state: Arc<Mutex<State>>) -> Result<()> {
async fn run_updater(lh: Lighthouse<TokioWebSocket>, shared_state: Arc<Mutex<State>>) -> Result<()> {
loop {
// Update the snake and render it
let frame = {
Expand Down Expand Up @@ -190,7 +190,7 @@ async fn main() -> Result<()> {
let auth = Authentication::new(&args.username, &args.token);
let state = Arc::new(Mutex::new(State::new()));

let mut lh = Lighthouse::connect_with_tokio_to(&args.url, auth).await?;
let lh = Lighthouse::connect_with_tokio_to(&args.url, auth).await?;
info!("Connected to the Lighthouse server");

let stream = lh.stream_model().await?;
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-client/examples/stress_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lighthouse_client::{protocol::{Authentication, Frame}, Lighthouse, Result, T
use tokio::time::{self, Instant};
use tracing::info;

async fn run(mut lh: Lighthouse<TokioWebSocket>, delay_ms: Option<u64>) -> Result<()> {
async fn run(lh: Lighthouse<TokioWebSocket>, delay_ms: Option<u64>) -> Result<()> {
info!("Connected to the Lighthouse server");

let mut last_second = Instant::now();
Expand Down
36 changes: 18 additions & 18 deletions lighthouse-client/src/lighthouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,81 +116,81 @@ impl<S> Lighthouse<S>
}

/// Replaces the user's lighthouse model with the given frame.
pub async fn put_model(&mut self, frame: Frame) -> Result<ServerMessage<()>> {
pub async fn put_model(&self, frame: Frame) -> Result<ServerMessage<()>> {
let username = self.authentication.username.clone();
self.put(&["user", username.as_str(), "model"], Model::Frame(frame)).await
}

/// Requests a stream of events (including key/controller events) for the user's lighthouse model.
pub async fn stream_model(&mut self) -> Result<impl Stream<Item = Result<ServerMessage<Model>>>> {
pub async fn stream_model(&self) -> Result<impl Stream<Item = Result<ServerMessage<Model>>>> {
let username = self.authentication.username.clone();
self.stream(&["user", username.as_str(), "model"], ()).await
}

/// Fetches lamp server metrics.
pub async fn get_laser_metrics(&mut self) -> Result<ServerMessage<LaserMetrics>> {
pub async fn get_laser_metrics(&self) -> Result<ServerMessage<LaserMetrics>> {
self.get(&["metrics", "laser"]).await
}

/// Combines PUT and CREATE. Requires CREATE and WRITE permission.
pub async fn post<P>(&mut self, path: &[&str], payload: P) -> Result<ServerMessage<()>>
pub async fn post<P>(&self, path: &[&str], payload: P) -> Result<ServerMessage<()>>
where
P: Serialize {
self.perform(&Verb::Post, path, payload).await
}

/// Updates the resource at the given path with the given payload. Requires WRITE permission.
pub async fn put<P>(&mut self, path: &[&str], payload: P) -> Result<ServerMessage<()>>
pub async fn put<P>(&self, path: &[&str], payload: P) -> Result<ServerMessage<()>>
where
P: Serialize {
self.perform(&Verb::Put, path, payload).await
}

/// Creates a resource at the given path. Requires CREATE permission.
pub async fn create(&mut self, path: &[&str]) -> Result<ServerMessage<()>> {
pub async fn create(&self, path: &[&str]) -> Result<ServerMessage<()>> {
self.perform(&Verb::Create, path, ()).await
}

/// Deletes a resource at the given path. Requires DELETE permission.
pub async fn delete(&mut self, path: &[&str]) -> Result<ServerMessage<()>> {
pub async fn delete(&self, path: &[&str]) -> Result<ServerMessage<()>> {
self.perform(&Verb::Delete, path, ()).await
}

/// Creates a directory at the given path. Requires CREATE permission.
pub async fn mkdir(&mut self, path: &[&str]) -> Result<ServerMessage<()>> {
pub async fn mkdir(&self, path: &[&str]) -> Result<ServerMessage<()>> {
self.perform(&Verb::Mkdir, path, ()).await
}

/// Lists the directory tree at the given path. Requires READ permission.
pub async fn list(&mut self, path: &[&str]) -> Result<ServerMessage<DirectoryTree>> {
pub async fn list(&self, path: &[&str]) -> Result<ServerMessage<DirectoryTree>> {
self.perform(&Verb::List, path, ()).await
}

/// Gets the resource at the given path. Requires READ permission.
pub async fn get<R>(&mut self, path: &[&str]) -> Result<ServerMessage<R>>
pub async fn get<R>(&self, path: &[&str]) -> Result<ServerMessage<R>>
where
R: for<'de> Deserialize<'de> {
self.perform(&Verb::Get, path, ()).await
}

/// Links the given source to the given destination path.
pub async fn link(&mut self, src_path: &[&str], dest_path: &[&str]) -> Result<ServerMessage<()>> {
pub async fn link(&self, src_path: &[&str], dest_path: &[&str]) -> Result<ServerMessage<()>> {
self.perform(&Verb::Link, dest_path, src_path).await
}

/// Unlinks the given source from the given destination path.
pub async fn unlink(&mut self, src_path: &[&str], dest_path: &[&str]) -> Result<ServerMessage<()>> {
pub async fn unlink(&self, src_path: &[&str], dest_path: &[&str]) -> Result<ServerMessage<()>> {
self.perform(&Verb::Unlink, dest_path, src_path).await
}

/// Stops the given stream.
pub async fn stop(&mut self, path: &[&str]) -> Result<ServerMessage<()>> {
pub async fn stop(&self, path: &[&str]) -> Result<ServerMessage<()>> {
self.perform(&Verb::Stop, path, ()).await
}

/// Performs a single request to the given path with the given payload.
#[tracing::instrument(skip(self, payload))]
pub async fn perform<P, R>(&mut self, verb: &Verb, path: &[&str], payload: P) -> Result<ServerMessage<R>>
pub async fn perform<P, R>(&self, verb: &Verb, path: &[&str], payload: P) -> Result<ServerMessage<R>>
where
P: Serialize,
R: for<'de> Deserialize<'de> {
Expand All @@ -202,7 +202,7 @@ impl<S> Lighthouse<S>

/// Performs a STREAM request to the given path with the given payload.
#[tracing::instrument(skip(self, payload))]
pub async fn stream<P, R>(&mut self, path: &[&str], payload: P) -> Result<impl Stream<Item = Result<ServerMessage<R>>>>
pub async fn stream<P, R>(&self, path: &[&str], payload: P) -> Result<impl Stream<Item = Result<ServerMessage<R>>>>
where
P: Serialize,
R: for<'de> Deserialize<'de> {
Expand All @@ -213,7 +213,7 @@ impl<S> Lighthouse<S>
}

/// Sends a request to the given path with the given payload.
async fn send_request<P>(&mut self, verb: &Verb, path: &[&str], payload: P) -> Result<i32>
async fn send_request<P>(&self, verb: &Verb, path: &[&str], payload: P) -> Result<i32>
where
P: Serialize {
let path = path.into_iter().map(|s| s.to_string()).collect();
Expand All @@ -231,7 +231,7 @@ impl<S> Lighthouse<S>
}

/// Sends a generic message to the lighthouse.
async fn send_message<P>(&mut self, message: &ClientMessage<P>) -> Result<()>
async fn send_message<P>(&self, message: &ClientMessage<P>) -> Result<()>
where
P: Serialize {
self.send_raw(rmp_serde::to_vec_named(message)?).await
Expand Down Expand Up @@ -287,7 +287,7 @@ impl<S> Lighthouse<S>
}

/// Sends raw bytes to the lighthouse via the WebSocket connection.
async fn send_raw(&mut self, bytes: impl Into<Vec<u8>> + Debug) -> Result<()> {
async fn send_raw(&self, bytes: impl Into<Vec<u8>> + Debug) -> Result<()> {
Ok(self.ws_sink.lock().await.send(Message::Binary(bytes.into())).await?)
}

Expand Down

0 comments on commit 18eca5b

Please sign in to comment.