Skip to content

Commit

Permalink
introduce interior mutability before it becomes a pita
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Mar 20, 2024
1 parent 00b25e4 commit 6a10f69
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
12 changes: 6 additions & 6 deletions crates/re_query2/benches/latest_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ struct SavePoint {
}

fn query_and_visit_points(store: &DataStore, paths: &[EntityPath]) -> Vec<SavePoint> {
let mut resolver = PromiseResolver::default();
let resolver = PromiseResolver::default();

let timeline_frame_nr = Timeline::new("frame_nr", TimeType::Sequence);
let query = LatestAtQuery::new(timeline_frame_nr, NUM_FRAMES_POINTS as i64 / 2);
Expand All @@ -286,11 +286,11 @@ fn query_and_visit_points(store: &DataStore, paths: &[EntityPath]) -> Vec<SavePo
let colors = results.get_optional::<Color>();

let points = points
.iter_dense::<Position2D>(&mut resolver)
.iter_dense::<Position2D>(&resolver)
.flatten()
.unwrap();

let colors = colors.iter_dense::<Color>(&mut resolver).flatten().unwrap();
let colors = colors.iter_dense::<Color>(&resolver).flatten().unwrap();
let color_default_fn = || Color::from(0xFF00FFFF);

for (point, color) in clamped_zip_1x1(points, colors, color_default_fn) {
Expand All @@ -309,7 +309,7 @@ struct SaveString {
}

fn query_and_visit_strings(store: &DataStore, paths: &[EntityPath]) -> Vec<SaveString> {
let mut resolver = PromiseResolver::default();
let resolver = PromiseResolver::default();

let timeline_frame_nr = Timeline::new("frame_nr", TimeType::Sequence);
let query = LatestAtQuery::new(timeline_frame_nr, NUM_FRAMES_STRINGS as i64 / 2);
Expand All @@ -328,11 +328,11 @@ fn query_and_visit_strings(store: &DataStore, paths: &[EntityPath]) -> Vec<SaveS
let colors = results.get_optional::<Text>();

let points = points
.iter_dense::<Position2D>(&mut resolver)
.iter_dense::<Position2D>(&resolver)
.flatten()
.unwrap();

let labels = colors.iter_dense::<Text>(&mut resolver).flatten().unwrap();
let labels = colors.iter_dense::<Text>(&resolver).flatten().unwrap();
let label_default_fn = || Text(String::new().into());

for (_point, label) in clamped_zip_1x1(points, labels, label_default_fn) {
Expand Down
8 changes: 4 additions & 4 deletions crates/re_query2/examples/latest_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() -> anyhow::Result<()> {
let store = store()?;
eprintln!("store:\n{}", store.to_data_table()?);

let mut resolver = PromiseResolver::default();
let resolver = PromiseResolver::default();

let entity_path = "points";
let timeline = Timeline::new("frame_nr", TimeType::Sequence);
Expand Down Expand Up @@ -54,7 +54,7 @@ fn main() -> anyhow::Result<()> {
// Our IDL doesn't support nullable instances at the moment -- so for the foreseeable future you probably
// shouldn't be using anything but `iter_dense`.

let points = match points.iter_dense::<MyPoint>(&mut resolver).flatten() {
let points = match points.iter_dense::<MyPoint>(&resolver).flatten() {
PromiseResult::Pending => {
// Handle the fact that the data isn't ready appropriately.
return Ok(());
Expand All @@ -63,7 +63,7 @@ fn main() -> anyhow::Result<()> {
PromiseResult::Error(err) => return Err(err.into()),
};

let colors = match colors.iter_dense::<MyColor>(&mut resolver).flatten() {
let colors = match colors.iter_dense::<MyColor>(&resolver).flatten() {
PromiseResult::Pending => {
// Handle the fact that the data isn't ready appropriately.
return Ok(());
Expand All @@ -72,7 +72,7 @@ fn main() -> anyhow::Result<()> {
PromiseResult::Error(err) => return Err(err.into()),
};

let labels = match labels.iter_sparse::<MyLabel>(&mut resolver).flatten() {
let labels = match labels.iter_sparse::<MyLabel>(&resolver).flatten() {
PromiseResult::Pending => {
// Handle the fact that the data isn't ready appropriately.
return Ok(());
Expand Down
8 changes: 4 additions & 4 deletions crates/re_query2/src/latest_at/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl LatestAtComponentResults {
#[inline]
pub fn to_dense<C: Component>(
&self,
resolver: &mut PromiseResolver,
resolver: &PromiseResolver,
) -> PromiseResult<DeserializationResult<Vec<C>>> {
if let Some(cell) = self.promise.as_ref() {
resolver.resolve(cell).map(|cell| {
Expand All @@ -171,7 +171,7 @@ impl LatestAtComponentResults {
#[inline]
pub fn iter_dense<C: Component>(
&self,
resolver: &mut PromiseResolver,
resolver: &PromiseResolver,
) -> PromiseResult<DeserializationResult<impl ExactSizeIterator<Item = C>>> {
self.to_dense(resolver)
.map(|data| data.map(|data| data.into_iter()))
Expand All @@ -186,7 +186,7 @@ impl LatestAtComponentResults {
#[inline]
pub fn to_sparse<C: Component>(
&self,
resolver: &mut PromiseResolver,
resolver: &PromiseResolver,
) -> PromiseResult<DeserializationResult<Vec<Option<C>>>> {
// Manufactured empty result.
if self.promise.is_none() {
Expand All @@ -213,7 +213,7 @@ impl LatestAtComponentResults {
#[inline]
pub fn iter_sparse<C: Component>(
&self,
resolver: &mut PromiseResolver,
resolver: &PromiseResolver,
) -> PromiseResult<DeserializationResult<impl ExactSizeIterator<Item = Option<C>>>> {
self.to_sparse(resolver)
.map(|data| data.map(|data| data.into_iter()))
Expand Down
4 changes: 3 additions & 1 deletion crates/re_query2/src/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ impl PromiseResolver {
/// idempotent (the [`PromiseResolver`] keeps track of the state of all [`Promise`]s, both
/// pending and already resolved).
#[inline]
pub fn resolve(&mut self, promise: &Promise) -> PromiseResult<DataCell> {
pub fn resolve(&self, promise: &Promise) -> PromiseResult<DataCell> {
// NOTE: we're pretending there's gonna be some kind of interior mutability when
// everything's said and done.
_ = self;
_ = promise.id;
PromiseResult::Ready(promise.source.clone())
Expand Down
18 changes: 9 additions & 9 deletions crates/re_query2/tests/latest_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use re_types_core::Loggable as _;

#[test]
fn simple_query() -> anyhow::Result<()> {
let mut resolver = PromiseResolver::default();
let resolver = PromiseResolver::default();

let mut store = DataStore::new(
re_log_types::StoreId::random(re_log_types::StoreKind::Recording),
Expand Down Expand Up @@ -62,13 +62,13 @@ fn simple_query() -> anyhow::Result<()> {

let points = results.get_required::<MyPoint>()?;
let point_data = points
.iter_dense::<MyPoint>(&mut resolver)
.iter_dense::<MyPoint>(&resolver)
.flatten()
.unwrap();

let colors = results.get_optional::<MyColor>();
let color_data = colors
.iter_sparse::<MyColor>(&mut resolver)
.iter_sparse::<MyColor>(&resolver)
.flatten()
.unwrap();
let color_default_fn = || Some(MyColor::from(0xFF00FFFF));
Expand All @@ -85,7 +85,7 @@ fn simple_query() -> anyhow::Result<()> {

#[test]
fn static_query() -> anyhow::Result<()> {
let mut resolver = PromiseResolver::default();
let resolver = PromiseResolver::default();

let mut store = DataStore::new(
re_log_types::StoreId::random(re_log_types::StoreKind::Recording),
Expand Down Expand Up @@ -138,13 +138,13 @@ fn static_query() -> anyhow::Result<()> {

let points = results.get_required::<MyPoint>()?;
let point_data = points
.iter_dense::<MyPoint>(&mut resolver)
.iter_dense::<MyPoint>(&resolver)
.flatten()
.unwrap();

let colors = results.get_optional::<MyColor>();
let color_data = colors
.iter_sparse::<MyColor>(&mut resolver)
.iter_sparse::<MyColor>(&resolver)
.flatten()
.unwrap();
let color_default_fn = || Some(MyColor::from(0xFF00FFFF));
Expand All @@ -161,7 +161,7 @@ fn static_query() -> anyhow::Result<()> {

#[test]
fn no_instance_join_query() -> anyhow::Result<()> {
let mut resolver = PromiseResolver::default();
let resolver = PromiseResolver::default();

let mut store = DataStore::new(
re_log_types::StoreId::random(re_log_types::StoreKind::Recording),
Expand Down Expand Up @@ -213,13 +213,13 @@ fn no_instance_join_query() -> anyhow::Result<()> {

let points = results.get_required::<MyPoint>()?;
let point_data = points
.iter_dense::<MyPoint>(&mut resolver)
.iter_dense::<MyPoint>(&resolver)
.flatten()
.unwrap();

let colors = results.get_optional::<MyColor>();
let color_data = colors
.iter_sparse::<MyColor>(&mut resolver)
.iter_sparse::<MyColor>(&resolver)
.flatten()
.unwrap();
let color_default_fn = || Some(MyColor::from(0xFF00FFFF));
Expand Down

0 comments on commit 6a10f69

Please sign in to comment.