1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

wrap sendprop definition in an Rc

This commit is contained in:
Robin Appelman 2019-08-27 00:48:43 +02:00
commit a7702856d0
3 changed files with 22 additions and 21 deletions

View file

@ -8,6 +8,7 @@ use crate::demo::sendprop::{SendProp, SendPropDefinition, SendPropValue};
use crate::{MalformedDemoError, Parse, ParseError, ParserState, ReadResult, Result, Stream};
use std::collections::HashMap;
use std::fmt;
use std::hint::unreachable_unchecked;
use std::num::ParseIntError;
use std::rc::Rc;
use std::str::FromStr;
@ -69,7 +70,7 @@ impl PacketEntity {
fn get_prop_by_definition(&mut self, definition: &SendPropDefinition) -> Option<&mut SendProp> {
self.props
.iter_mut()
.find(|prop| prop.definition.eq(definition))
.find(|prop| prop.definition.as_ref().eq(definition))
}
pub fn apply_update(&mut self, props: Vec<SendProp>) {
@ -90,7 +91,7 @@ fn read_bit_var<T: BitReadSized<LittleEndian>>(stream: &mut Stream) -> ReadResul
1 => 8,
2 => 12,
3 => 32,
_ => unreachable!(),
_ => unsafe { unreachable_unchecked() },
};
stream.read_sized(bits)
}
@ -247,7 +248,7 @@ impl PacketEntitiesMessage {
Some(definition) => {
let value = SendPropValue::parse(stream, definition)?;
props.push(SendProp {
definition: definition.clone(),
definition: Rc::clone(definition),
value,
});
}

View file

@ -60,7 +60,7 @@ impl From<String> for SendTableName {
#[derive(Debug)]
pub struct ParseSendTable {
pub name: SendTableName,
pub props: Vec<SendPropDefinition>,
pub props: Vec<Rc<SendPropDefinition>>,
pub needs_decoder: bool,
}
@ -86,9 +86,9 @@ impl Parse for ParseSendTable {
return Err(MalformedSendPropDefinitionError::UntypedArray.into());
}
array_element_prop = None;
props.push(prop.with_array_property(array_element));
props.push(Rc::new(prop.with_array_property(array_element)));
} else {
props.push(prop);
props.push(Rc::new(prop));
}
}
@ -101,7 +101,7 @@ impl Parse for ParseSendTable {
}
impl ParseSendTable {
pub fn flatten_props(&self, tables: &[ParseSendTable]) -> Vec<SendPropDefinition> {
pub fn flatten_props(&self, tables: &[ParseSendTable]) -> Vec<Rc<SendPropDefinition>> {
let mut flat = Vec::with_capacity(32);
self.get_all_props(tables, &self.get_excludes(tables), &mut flat);
@ -116,7 +116,7 @@ impl ParseSendTable {
}
}
flat.into_iter().map(|prop| prop.clone()).collect()
flat.into_iter().map(|prop| Rc::clone(&prop)).collect()
}
fn get_excludes<'a>(&'a self, tables: &'a [ParseSendTable]) -> Vec<Exclude<'a>> {
@ -137,11 +137,11 @@ impl ParseSendTable {
}
// TODO: below is a direct port from the js which is a direct port from C++ and not very optimal
fn get_all_props<'a>(
&'a self,
tables: &'a [ParseSendTable],
fn get_all_props(
&self,
tables: &[ParseSendTable],
excludes: &[Exclude],
props: &mut Vec<&'a SendPropDefinition>,
props: &mut Vec<Rc<SendPropDefinition>>,
) {
let mut local_props = Vec::new();
@ -149,12 +149,12 @@ impl ParseSendTable {
props.extend_from_slice(&local_props);
}
fn get_all_props_iterator_props<'a>(
&'a self,
tables: &'a [ParseSendTable],
fn get_all_props_iterator_props(
&self,
tables: &[ParseSendTable],
excludes: &[Exclude],
local_props: &mut Vec<&'a SendPropDefinition>,
props: &mut Vec<&'a SendPropDefinition>,
local_props: &mut Vec<Rc<SendPropDefinition>>,
props: &mut Vec<Rc<SendPropDefinition>>,
) {
self.props
.iter()
@ -168,7 +168,7 @@ impl ParseSendTable {
table.get_all_props(tables, excludes, props);
}
} else {
local_props.push(prop);
local_props.push(Rc::clone(prop));
}
})
}
@ -189,9 +189,9 @@ impl<'a> Exclude<'a> {
#[derive(Debug)]
pub struct SendTable {
pub name: SendTableName,
pub props: Vec<SendPropDefinition>,
pub props: Vec<Rc<SendPropDefinition>>,
pub needs_decoder: bool,
pub flattened_props: Vec<SendPropDefinition>,
pub flattened_props: Vec<Rc<SendPropDefinition>>,
}
#[derive(Debug)]

View file

@ -488,7 +488,7 @@ impl From<Vec<SendPropValue>> for SendPropValue {
#[derive(Debug, Clone)]
pub struct SendProp {
pub definition: SendPropDefinition,
pub definition: Rc<SendPropDefinition>,
pub value: SendPropValue,
}