Skip to content

Commit d285982

Browse files
committed
Wrap TableName in a newtype
Per Mazdak's request on #2793
1 parent 5988270 commit d285982

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

crates/core/src/subscription/module_subscription_manager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use spacetimedb_data_structures::map::{Entry, IntMap};
2020
use spacetimedb_lib::metrics::ExecutionMetrics;
2121
use spacetimedb_lib::{AlgebraicValue, ConnectionId, Identity, ProductValue};
2222
use spacetimedb_primitives::{ColId, TableId};
23-
use spacetimedb_subscription::SubscriptionPlan;
23+
use spacetimedb_subscription::{SubscriptionPlan, TableName};
2424
use std::collections::BTreeSet;
2525
use std::sync::atomic::{AtomicBool, Ordering};
2626
use std::sync::Arc;
@@ -363,7 +363,7 @@ struct ClientQueryUpdate<F: WebsocketFormat> {
363363
struct ClientUpdate {
364364
id: ClientId,
365365
table_id: TableId,
366-
table_name: Arc<str>,
366+
table_name: TableName,
367367
update: FormatSwitch<ClientQueryUpdate<BsatnFormat>, ClientQueryUpdate<JsonFormat>>,
368368
}
369369

@@ -842,7 +842,7 @@ impl SubscriptionManager {
842842
// which involves cloning BSATN (binary) or product values (json).
843843
.fold(FoldState::default(), |mut acc, (qstate, plan)| {
844844
let table_id = plan.subscribed_table_id();
845-
let table_name = Arc::clone(plan.subscribed_table_name());
845+
let table_name = plan.subscribed_table_name().clone();
846846
// Store at most one copy for both the serialization to BSATN and JSON.
847847
// Each subscriber gets to pick which of these they want,
848848
// but we only fill `ops_bin_uncompressed` and `ops_json` at most once.

crates/subscription/src/lib.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,56 @@ impl Fragments {
211211
}
212212
}
213213

214+
/// Newtype wrapper for table names.
215+
///
216+
/// Uses an `Arc` internally, so `Clone` is cheap.
217+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
218+
pub struct TableName(Arc<str>);
219+
220+
impl From<Arc<str>> for TableName {
221+
fn from(name: Arc<str>) -> Self {
222+
TableName(name)
223+
}
224+
}
225+
226+
impl From<Box<str>> for TableName {
227+
fn from(name: Box<str>) -> Self {
228+
TableName(name.into())
229+
}
230+
}
231+
232+
impl From<String> for TableName {
233+
fn from(name: String) -> Self {
234+
TableName(name.into())
235+
}
236+
}
237+
238+
impl std::ops::Deref for TableName {
239+
type Target = str;
240+
fn deref(&self) -> &Self::Target {
241+
&*self.0
242+
}
243+
}
244+
245+
impl TableName {
246+
pub fn from_str(name: &str) -> Self {
247+
TableName(name.into())
248+
}
249+
}
250+
251+
impl std::fmt::Display for TableName {
252+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
253+
self.0.fmt(f)
254+
}
255+
}
256+
214257
/// A subscription defines a view over a table
215258
#[derive(Debug)]
216259
pub struct SubscriptionPlan {
217260
/// To which table are we subscribed?
218261
return_id: TableId,
219262
/// To which table are we subscribed?
220-
return_name: Arc<str>,
263+
return_name: TableName,
221264
/// A subscription can read from multiple tables.
222265
/// From which tables do we read?
223266
table_ids: Vec<TableId>,
@@ -243,7 +286,7 @@ impl SubscriptionPlan {
243286
}
244287

245288
/// To which table does this plan subscribe?
246-
pub fn subscribed_table_name(&self) -> &Arc<str> {
289+
pub fn subscribed_table_name(&self) -> &TableName {
247290
&self.return_name
248291
}
249292

@@ -333,7 +376,7 @@ impl SubscriptionPlan {
333376

334377
let mut subscriptions = vec![];
335378

336-
let return_name = Arc::<str>::from(return_name);
379+
let return_name = TableName::from(return_name);
337380

338381
for plan in plans {
339382
if has_non_index_join(&plan) {
@@ -346,7 +389,7 @@ impl SubscriptionPlan {
346389

347390
subscriptions.push(Self {
348391
return_id,
349-
return_name: Arc::clone(&return_name),
392+
return_name: return_name.clone(),
350393
table_ids,
351394
plan,
352395
fragments,

0 commit comments

Comments
 (0)