Get rid of some unnecessary clones

This commit is contained in:
Simon Ask Ulsnes 2024-02-04 09:38:59 +01:00
parent d94eff3861
commit 7071ddd276
3 changed files with 19 additions and 23 deletions

View file

@ -175,7 +175,7 @@ fn yaml_emitter_needs_mode_events(emitter: &mut Emitter) -> Option<Event> {
fn yaml_emitter_append_tag_directive(
emitter: &mut Emitter,
value: &TagDirective,
value: TagDirective,
allow_duplicates: bool,
) -> Result<(), EmitterError> {
for tag_directive in &emitter.tag_directives {
@ -186,7 +186,7 @@ fn yaml_emitter_append_tag_directive(
return yaml_emitter_set_emitter_error(emitter, "duplicate %TAG directive");
}
}
emitter.tag_directives.push(value.clone());
emitter.tag_directives.push(value);
Ok(())
}
@ -199,10 +199,10 @@ fn yaml_emitter_increase_indent(emitter: &mut Emitter, flow: bool, indentless: b
}
}
fn yaml_emitter_state_machine(
fn yaml_emitter_state_machine<'a>(
emitter: &mut Emitter,
event: &Event,
analysis: &mut Analysis,
event: &'a Event,
analysis: &mut Analysis<'a>,
) -> Result<(), EmitterError> {
match emitter.state {
EmitterState::StreamStart => yaml_emitter_emit_stream_start(emitter, event),
@ -303,9 +303,6 @@ fn yaml_emitter_emit_document_start(
implicit,
} = &event.data
{
let (version_directive, tag_directives, implicit) =
(*version_directive, tag_directives, *implicit);
let default_tag_directives: [TagDirective; 2] = [
// TODO: Avoid these heap allocations.
TagDirective {
@ -317,15 +314,15 @@ fn yaml_emitter_emit_document_start(
prefix: String::from("tag:yaml.org,2002:"),
},
];
let mut implicit = implicit;
let mut implicit = *implicit;
if let Some(version_directive) = version_directive {
yaml_emitter_analyze_version_directive(emitter, version_directive)?;
yaml_emitter_analyze_version_directive(emitter, *version_directive)?;
}
for tag_directive in tag_directives {
yaml_emitter_analyze_tag_directive(emitter, tag_directive)?;
yaml_emitter_append_tag_directive(emitter, tag_directive, false)?;
yaml_emitter_append_tag_directive(emitter, tag_directive.clone(), false)?;
}
for tag_directive in &default_tag_directives {
for tag_directive in default_tag_directives {
yaml_emitter_append_tag_directive(emitter, tag_directive, true)?;
}
if !first || emitter.canonical {

View file

@ -314,7 +314,7 @@ fn yaml_parser_load_sequence(
document.nodes.push(node);
let index: i32 = document.nodes.len() as i32;
yaml_parser_register_anchor(parser, document, index, anchor.clone())?;
yaml_parser_register_anchor(parser, document, index, anchor)?;
yaml_parser_load_node_add(document, ctx, index)?;
ctx.push(index);
Ok(())

View file

@ -852,7 +852,7 @@ fn yaml_parser_process_directives(
let mut tag_directives = Vec::with_capacity(16);
let mut token = PEEK_TOKEN(parser)?;
let mut token = PEEK_TOKEN_MUT(parser)?;
loop {
if !matches!(
@ -873,24 +873,23 @@ fn yaml_parser_process_directives(
major: *major,
minor: *minor,
});
} else if let TokenData::TagDirective { handle, prefix } = &token.data {
} else if let TokenData::TagDirective { handle, prefix } = &mut token.data {
let value = TagDirective {
// TODO: Get rid of these clones by consuming tokens by value.
handle: handle.clone(),
prefix: prefix.clone(),
handle: core::mem::take(handle),
prefix: core::mem::take(prefix),
};
let mark = token.start_mark;
yaml_parser_append_tag_directive(parser, &value, false, mark)?;
yaml_parser_append_tag_directive(parser, value.clone(), false, mark)?;
tag_directives.push(value);
}
SKIP_TOKEN(parser);
token = PEEK_TOKEN(parser)?;
token = PEEK_TOKEN_MUT(parser)?;
}
let start_mark = token.start_mark;
for default_tag_directive in &default_tag_directives {
for default_tag_directive in default_tag_directives {
yaml_parser_append_tag_directive(parser, default_tag_directive, true, start_mark)?;
}
@ -913,7 +912,7 @@ fn yaml_parser_process_directives(
fn yaml_parser_append_tag_directive(
parser: &mut Parser,
value: &TagDirective,
value: TagDirective,
allow_duplicates: bool,
mark: Mark,
) -> Result<(), ParserError> {
@ -925,6 +924,6 @@ fn yaml_parser_append_tag_directive(
return yaml_parser_set_parser_error("found duplicate %TAG directive", mark);
}
}
parser.tag_directives.push(value.clone());
parser.tag_directives.push(value);
Ok(())
}