Start stripping out lifetimes in InstanceSnapshot

This commit is contained in:
Lucien Greathouse
2019-11-19 13:05:18 -08:00
parent f32cb592e2
commit d2e2a13479
5 changed files with 23 additions and 29 deletions

View File

@@ -7,15 +7,13 @@ use serde::{Deserialize, Serialize};
use super::InstanceMetadata; use super::InstanceMetadata;
/// A lightweight description of what an instance should look like. Attempts to /// A lightweight description of what an instance should look like.
/// be somewhat memory efficient by borrowing from its source data, indicated by
/// the lifetime parameter `'source`.
/// ///
// Possible future improvements: // Possible future improvements:
// - Use refcounted/interned strings // - Use refcounted/interned strings
// - Replace use of RbxValue with a sum of RbxValue + borrowed value // - Replace use of RbxValue with a sum of RbxValue + borrowed value
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InstanceSnapshot<'source> { pub struct InstanceSnapshot {
/// A temporary ID applied to the snapshot that's used for Ref properties. /// A temporary ID applied to the snapshot that's used for Ref properties.
pub snapshot_id: Option<RbxId>, pub snapshot_id: Option<RbxId>,
@@ -23,10 +21,10 @@ pub struct InstanceSnapshot<'source> {
pub metadata: InstanceMetadata, pub metadata: InstanceMetadata,
/// Correpsonds to the Name property of the instance. /// Correpsonds to the Name property of the instance.
pub name: Cow<'source, str>, pub name: Cow<'static, str>,
/// Corresponds to the ClassName property of the instance. /// Corresponds to the ClassName property of the instance.
pub class_name: Cow<'source, str>, pub class_name: Cow<'static, str>,
/// All other properties of the instance, weakly-typed. /// All other properties of the instance, weakly-typed.
pub properties: HashMap<String, RbxValue>, pub properties: HashMap<String, RbxValue>,
@@ -34,11 +32,11 @@ pub struct InstanceSnapshot<'source> {
/// The children of the instance represented as more snapshots. /// The children of the instance represented as more snapshots.
/// ///
/// Order is relevant for Roblox instances! /// Order is relevant for Roblox instances!
pub children: Vec<InstanceSnapshot<'source>>, pub children: Vec<InstanceSnapshot>,
} }
impl<'source> InstanceSnapshot<'source> { impl InstanceSnapshot {
pub fn from_tree(tree: &RbxTree, id: RbxId) -> InstanceSnapshot<'static> { pub fn from_tree(tree: &RbxTree, id: RbxId) -> InstanceSnapshot {
let instance = tree let instance = tree
.get_instance(id) .get_instance(id)
.expect("instance did not exist in tree"); .expect("instance did not exist in tree");
@@ -61,8 +59,8 @@ impl<'source> InstanceSnapshot<'source> {
} }
} }
impl<'source> Default for InstanceSnapshot<'source> { impl Default for InstanceSnapshot {
fn default() -> InstanceSnapshot<'source> { fn default() -> InstanceSnapshot {
InstanceSnapshot { InstanceSnapshot {
snapshot_id: None, snapshot_id: None,
metadata: InstanceMetadata::default(), metadata: InstanceMetadata::default(),

View File

@@ -13,13 +13,13 @@ use super::{InstanceMetadata, InstanceSnapshot};
/// sure that another patch wasn't applied before this one that could cause a /// sure that another patch wasn't applied before this one that could cause a
/// conflict! /// conflict!
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct PatchSet<'a> { pub struct PatchSet {
pub removed_instances: Vec<RbxId>, pub removed_instances: Vec<RbxId>,
pub added_instances: Vec<PatchAdd<'a>>, pub added_instances: Vec<PatchAdd>,
pub updated_instances: Vec<PatchUpdate>, pub updated_instances: Vec<PatchUpdate>,
} }
impl<'a> PatchSet<'a> { impl<'a> PatchSet {
pub fn new() -> Self { pub fn new() -> Self {
PatchSet { PatchSet {
removed_instances: Vec::new(), removed_instances: Vec::new(),
@@ -31,9 +31,9 @@ impl<'a> PatchSet<'a> {
/// A patch containing an instance that was added to the tree. /// A patch containing an instance that was added to the tree.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PatchAdd<'a> { pub struct PatchAdd {
pub parent_id: RbxId, pub parent_id: RbxId,
pub instance: InstanceSnapshot<'a>, pub instance: InstanceSnapshot,
} }
/// A patch indicating that properties of an instance changed. /// A patch indicating that properties of an instance changed.

View File

@@ -10,11 +10,7 @@ use super::{
InstanceSnapshot, InstanceWithMeta, RojoTree, InstanceSnapshot, InstanceWithMeta, RojoTree,
}; };
pub fn compute_patch_set<'a>( pub fn compute_patch_set(snapshot: &InstanceSnapshot, tree: &RojoTree, id: RbxId) -> PatchSet {
snapshot: &'a InstanceSnapshot,
tree: &RojoTree,
id: RbxId,
) -> PatchSet<'a> {
let mut patch_set = PatchSet::new(); let mut patch_set = PatchSet::new();
let mut context = ComputePatchContext::default(); let mut context = ComputePatchContext::default();
@@ -69,12 +65,12 @@ fn rewrite_refs_in_snapshot(context: &ComputePatchContext, snapshot: &mut Instan
} }
} }
fn compute_patch_set_internal<'a>( fn compute_patch_set_internal(
context: &mut ComputePatchContext, context: &mut ComputePatchContext,
snapshot: &'a InstanceSnapshot, snapshot: &InstanceSnapshot,
tree: &RojoTree, tree: &RojoTree,
id: RbxId, id: RbxId,
patch_set: &mut PatchSet<'a>, patch_set: &mut PatchSet,
) { ) {
if let Some(snapshot_id) = snapshot.snapshot_id { if let Some(snapshot_id) = snapshot.snapshot_id {
context.snapshot_id_to_instance_id.insert(snapshot_id, id); context.snapshot_id_to_instance_id.insert(snapshot_id, id);
@@ -154,12 +150,12 @@ fn compute_property_patches(
}); });
} }
fn compute_children_patches<'a>( fn compute_children_patches(
context: &mut ComputePatchContext, context: &mut ComputePatchContext,
snapshot: &'a InstanceSnapshot, snapshot: &InstanceSnapshot,
tree: &RojoTree, tree: &RojoTree,
id: RbxId, id: RbxId,
patch_set: &mut PatchSet<'a>, patch_set: &mut PatchSet,
) { ) {
let instance = tree let instance = tree
.get_instance(id) .get_instance(id)

View File

@@ -102,7 +102,7 @@ struct JsonModelCore {
} }
impl JsonModelCore { impl JsonModelCore {
fn into_snapshot(self, name: String) -> InstanceSnapshot<'static> { fn into_snapshot(self, name: String) -> InstanceSnapshot {
let class_name = self.class_name; let class_name = self.class_name;
let children = self let children = self

View File

@@ -7,7 +7,7 @@ use crate::{
use super::{context::InstanceSnapshotContext, error::SnapshotError}; use super::{context::InstanceSnapshotContext, error::SnapshotError};
pub type SnapshotInstanceResult<'a> = Result<Option<InstanceSnapshot<'a>>, SnapshotError>; pub type SnapshotInstanceResult<'a> = Result<Option<InstanceSnapshot>, SnapshotError>;
pub type SnapshotFileResult = Option<(String, VfsSnapshot)>; pub type SnapshotFileResult = Option<(String, VfsSnapshot)>;
pub trait SnapshotMiddleware { pub trait SnapshotMiddleware {