Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove dependency on async-trait #54

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion linkup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
async-trait = "0.1.68"
getrandom = { version = "0.2.8", features = ["js"] }
hex = "0.4"
rand = "0.8"
Expand Down
13 changes: 8 additions & 5 deletions linkup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mod name_gen;
mod session;
mod session_allocator;

use async_trait::async_trait;
use std::future::Future;

use rand::Rng;
use thiserror::Error;

Expand All @@ -28,11 +29,13 @@ pub enum SessionError {
ConfigErr(String),
}

#[async_trait(?Send)]
// Since this trait is theoretically public (even though, the idea is for it to be used by the other modules within
// this workspace), we should return `impl Future` instead of having `async fn` so that we can add and ensure
// any desired bounds.
pub trait StringStore {
async fn get(&self, key: String) -> Result<Option<String>, SessionError>;
async fn exists(&self, key: String) -> Result<bool, SessionError>;
async fn put(&self, key: String, value: String) -> Result<(), SessionError>;
fn get(&self, key: String) -> impl Future<Output = Result<Option<String>, SessionError>>;
fn exists(&self, key: String) -> impl Future<Output = Result<bool, SessionError>>;
fn put(&self, key: String, value: String) -> impl Future<Output = Result<(), SessionError>>;
}

#[derive(PartialEq)]
Expand Down
3 changes: 0 additions & 3 deletions linkup/src/memory_session_store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{collections::HashMap, sync::RwLock};

use async_trait::async_trait;

use crate::{SessionError, StringStore};

pub struct MemoryStringStore {
Expand All @@ -22,7 +20,6 @@ impl Default for MemoryStringStore {
}
}

#[async_trait(?Send)]
impl StringStore for MemoryStringStore {
async fn get(&self, key: String) -> Result<Option<String>, SessionError> {
match self.store.read() {
Expand Down
8 changes: 4 additions & 4 deletions linkup/src/session_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::{
StringStore,
};

pub struct SessionAllocator<'a> {
store: &'a dyn StringStore,
pub struct SessionAllocator<'a, S: StringStore> {
store: &'a S,
}

impl<'a> SessionAllocator<'a> {
pub fn new(store: &'a dyn StringStore) -> Self {
impl<'a, S: StringStore> SessionAllocator<'a, S> {
pub fn new(store: &'a S) -> Self {
Self { store }
}

Expand Down
3 changes: 1 addition & 2 deletions worker/src/kv_store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use linkup::{SessionError, StringStore};
use worker::{async_trait::async_trait, kv::KvStore};
use worker::kv::KvStore;

pub struct CfWorkerStringStore {
kv: KvStore,
Expand All @@ -11,7 +11,6 @@ impl CfWorkerStringStore {
}
}

#[async_trait(?Send)]
impl StringStore for CfWorkerStringStore {
async fn get(&self, key: String) -> Result<Option<String>, SessionError> {
match self.kv.get(key.as_str()).text().await {
Expand Down
12 changes: 6 additions & 6 deletions worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ mod kv_store;
mod utils;
mod ws;

async fn linkup_session_handler<'a>(
async fn linkup_session_handler<'a, S: StringStore>(
mut req: Request,
sessions: &'a SessionAllocator<'a>,
sessions: &'a SessionAllocator<'a, S>,
) -> Result<Response> {
let body_bytes = match req.bytes().await {
Ok(bytes) => bytes,
Expand All @@ -39,9 +39,9 @@ async fn linkup_session_handler<'a>(
}
}

async fn linkup_preview_handler<'a>(
async fn linkup_preview_handler<'a, S: StringStore>(
mut req: Request,
sessions: &'a SessionAllocator<'a>,
sessions: &'a SessionAllocator<'a, S>,
) -> Result<Response> {
let body_bytes = match req.bytes().await {
Ok(bytes) => bytes,
Expand Down Expand Up @@ -110,9 +110,9 @@ async fn set_cached_req(
Ok(resp)
}

async fn linkup_request_handler<'a>(
async fn linkup_request_handler<'a, S: StringStore>(
mut req: Request,
sessions: &'a SessionAllocator<'a>,
sessions: &'a SessionAllocator<'a, S>,
) -> Result<Response> {
let url = match req.url() {
Ok(url) => url.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions worker/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use futures::{

use crate::http_util::plaintext_error;

pub async fn linkup_ws_handler<'a>(
pub async fn linkup_ws_handler<'a, S: StringStore>(
req: Request,
sessions: &'a SessionAllocator<'a>,
sessions: &'a SessionAllocator<'a, S>,
) -> Result<Response> {
let url = match req.url() {
Ok(url) => url.to_string(),
Expand Down