diff --git a/src/api.rs b/src/api.rs index 03fd21b..7108c6c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,13 +1,12 @@ use crate::externs::{free, malloc, memcpy, memmove, memset, realloc, strdup, strlen}; use crate::ops::{ForceAdd as _, ForceMul as _}; -use crate::yaml::{size_t, yaml_char_t, YamlEventData, YamlTokenData}; +use crate::yaml::{size_t, yaml_char_t, YamlEventData, YamlNodeData, YamlTokenData}; use crate::{ libc, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t, yaml_encoding_t, yaml_event_t, yaml_mapping_style_t, yaml_mark_t, yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_parser_state_t, yaml_parser_t, yaml_read_handler_t, yaml_scalar_style_t, - yaml_sequence_style_t, yaml_simple_key_t, yaml_tag_directive_t, yaml_token_t, + yaml_sequence_style_t, yaml_simple_key_t, yaml_stack_t, yaml_tag_directive_t, yaml_token_t, yaml_version_directive_t, yaml_write_handler_t, PointerExt, YAML_ANY_ENCODING, - YAML_MAPPING_NODE, YAML_SCALAR_NODE, YAML_SEQUENCE_NODE, }; use core::mem::{size_of, MaybeUninit}; use core::ptr::{self, addr_of_mut}; @@ -1090,18 +1089,18 @@ pub unsafe fn yaml_document_delete(document: &mut yaml_document_t) { while !STACK_EMPTY!(document.nodes) { let mut node = POP!(document.nodes); yaml_free(node.tag as *mut libc::c_void); - match node.type_ { - YAML_SCALAR_NODE => { - yaml_free(node.data.scalar.value as *mut libc::c_void); + match node.data { + YamlNodeData::NoNode => { + assert!(false); } - YAML_SEQUENCE_NODE => { - STACK_DEL!(node.data.sequence.items); + YamlNodeData::Scalar { ref value, .. } => { + yaml_free(*value as *mut libc::c_void); } - YAML_MAPPING_NODE => { - STACK_DEL!(node.data.mapping.pairs); + YamlNodeData::Sequence { ref mut items, .. } => { + STACK_DEL!(items); } - _ => { - __assert!(false); + YamlNodeData::Mapping { ref mut pairs, .. } => { + STACK_DEL!(pairs); } } } @@ -1174,8 +1173,6 @@ pub unsafe fn yaml_document_add_scalar( }; let mut tag_copy: *mut yaml_char_t = ptr::null_mut::(); let mut value_copy: *mut yaml_char_t = ptr::null_mut::(); - let mut node = MaybeUninit::::uninit(); - let node = node.as_mut_ptr(); __assert!(!value.is_null()); if tag.is_null() { tag = b"tag:yaml.org,2002:str\0" as *const u8 as *const libc::c_char as *mut yaml_char_t; @@ -1194,19 +1191,17 @@ pub unsafe fn yaml_document_add_scalar( length as libc::c_ulong, ); *value_copy.wrapping_offset(length as isize) = b'\0'; - memset( - node as *mut libc::c_void, - 0, - size_of::() as libc::c_ulong, - ); - (*node).type_ = YAML_SCALAR_NODE; - (*node).tag = tag_copy; - (*node).start_mark = mark; - (*node).end_mark = mark; - (*node).data.scalar.value = value_copy; - (*node).data.scalar.length = length as size_t; - (*node).data.scalar.style = style; - PUSH!(document.nodes, *node); + let node = yaml_node_t { + data: YamlNodeData::Scalar { + value: value_copy, + length: length as size_t, + style: style, + }, + tag: tag_copy, + start_mark: mark, + end_mark: mark, + }; + PUSH!(document.nodes, node); return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int; } } @@ -1243,8 +1238,6 @@ pub unsafe fn yaml_document_add_sequence( end: ptr::null_mut::(), top: ptr::null_mut::(), }; - let mut node = MaybeUninit::::uninit(); - let node = node.as_mut_ptr(); if tag.is_null() { tag = b"tag:yaml.org,2002:seq\0" as *const u8 as *const libc::c_char as *mut yaml_char_t; } @@ -1252,20 +1245,20 @@ pub unsafe fn yaml_document_add_sequence( tag_copy = yaml_strdup(tag); if !tag_copy.is_null() { STACK_INIT!(items, yaml_node_item_t); - memset( - node as *mut libc::c_void, - 0, - size_of::() as libc::c_ulong, - ); - (*node).type_ = YAML_SEQUENCE_NODE; - (*node).tag = tag_copy; - (*node).start_mark = mark; - (*node).end_mark = mark; - (*node).data.sequence.items.start = items.start; - (*node).data.sequence.items.end = items.end; - (*node).data.sequence.items.top = items.start; - (*node).data.sequence.style = style; - PUSH!(document.nodes, *node); + let node = yaml_node_t { + data: YamlNodeData::Sequence { + items: yaml_stack_t { + start: items.start, + end: items.end, + top: items.start, + }, + style, + }, + tag: tag_copy, + start_mark: mark, + end_mark: mark, + }; + PUSH!(document.nodes, node); return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int; } } @@ -1301,8 +1294,6 @@ pub unsafe fn yaml_document_add_mapping( end: ptr::null_mut::(), top: ptr::null_mut::(), }; - let mut node = MaybeUninit::::uninit(); - let node = node.as_mut_ptr(); if tag.is_null() { tag = b"tag:yaml.org,2002:map\0" as *const u8 as *const libc::c_char as *mut yaml_char_t; } @@ -1310,20 +1301,22 @@ pub unsafe fn yaml_document_add_mapping( tag_copy = yaml_strdup(tag); if !tag_copy.is_null() { STACK_INIT!(pairs, yaml_node_pair_t); - memset( - node as *mut libc::c_void, - 0, - size_of::() as libc::c_ulong, - ); - (*node).type_ = YAML_MAPPING_NODE; - (*node).tag = tag_copy; - (*node).start_mark = mark; - (*node).end_mark = mark; - (*node).data.mapping.pairs.start = pairs.start; - (*node).data.mapping.pairs.end = pairs.end; - (*node).data.mapping.pairs.top = pairs.start; - (*node).data.mapping.style = style; - PUSH!(document.nodes, *node); + + let node = yaml_node_t { + data: YamlNodeData::Mapping { + pairs: yaml_stack_t { + start: pairs.start, + end: pairs.end, + top: pairs.start, + }, + style, + }, + tag: tag_copy, + start_mark: mark, + end_mark: mark, + }; + + PUSH!(document.nodes, node); return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int; } } @@ -1342,20 +1335,18 @@ pub unsafe fn yaml_document_append_sequence_item( sequence > 0 && (document.nodes.start).wrapping_offset(sequence as isize) <= document.nodes.top ); - __assert!( - (*(document.nodes.start).wrapping_offset((sequence - 1) as isize)).type_ - == YAML_SEQUENCE_NODE - ); + __assert!(matches!( + (*(document.nodes.start).wrapping_offset((sequence - 1) as isize)).data, + YamlNodeData::Sequence { .. } + )); __assert!( item > 0 && (document.nodes.start).wrapping_offset(item as isize) <= document.nodes.top ); - PUSH!( - (*(document.nodes.start).wrapping_offset((sequence - 1) as isize)) - .data - .sequence - .items, - item - ); + if let YamlNodeData::Sequence { ref mut items, .. } = + (*(document.nodes.start).wrapping_offset((sequence - 1) as isize)).data + { + PUSH!(*items, item); + } Ok(()) } @@ -1370,10 +1361,10 @@ pub unsafe fn yaml_document_append_mapping_pair( mapping > 0 && (document.nodes.start).wrapping_offset(mapping as isize) <= document.nodes.top ); - __assert!( - (*(document.nodes.start).wrapping_offset((mapping - 1) as isize)).type_ - == YAML_MAPPING_NODE - ); + __assert!(matches!( + (*(document.nodes.start).wrapping_offset((mapping - 1) as isize)).data, + YamlNodeData::Mapping { .. } + )); __assert!( key > 0 && (document.nodes.start).wrapping_offset(key as isize) <= document.nodes.top ); @@ -1381,12 +1372,10 @@ pub unsafe fn yaml_document_append_mapping_pair( value > 0 && (document.nodes.start).wrapping_offset(value as isize) <= document.nodes.top ); let pair = yaml_node_pair_t { key, value }; - PUSH!( - (*(document.nodes.start).wrapping_offset((mapping - 1) as isize)) - .data - .mapping - .pairs, - pair - ); + if let YamlNodeData::Mapping { ref mut pairs, .. } = + (*(document.nodes.start).wrapping_offset((mapping - 1) as isize)).data + { + PUSH!(*pairs, pair); + } Ok(()) } diff --git a/src/dumper.rs b/src/dumper.rs index 87a6f80..ca8c923 100644 --- a/src/dumper.rs +++ b/src/dumper.rs @@ -4,8 +4,7 @@ use crate::fmt::WriteToPtr; use crate::ops::ForceMul as _; use crate::yaml::{ yaml_anchors_t, yaml_char_t, yaml_document_t, yaml_emitter_t, yaml_event_t, yaml_node_item_t, - yaml_node_pair_t, yaml_node_t, YamlEventData, YAML_ANY_ENCODING, YAML_MAPPING_NODE, - YAML_SCALAR_NODE, YAML_SEQUENCE_NODE, + yaml_node_pair_t, yaml_node_t, YamlEventData, YamlNodeData, YAML_ANY_ENCODING, }; use crate::{libc, yaml_document_delete, yaml_emitter_emit, PointerExt}; use core::mem::size_of; @@ -133,21 +132,21 @@ unsafe fn yaml_emitter_delete_document_and_anchors(emitter: &mut yaml_emitter_t) .wrapping_offset(index as isize) < (*emitter.document).nodes.top { - let mut node: yaml_node_t = *(*emitter.document) + let node: *mut yaml_node_t = (*emitter.document) .nodes .start .wrapping_offset(index as isize); if !(*emitter.anchors.wrapping_offset(index as isize)).serialized { - yaml_free(node.tag as *mut libc::c_void); - if node.type_ == YAML_SCALAR_NODE { - yaml_free(node.data.scalar.value as *mut libc::c_void); + yaml_free((*node).tag as *mut libc::c_void); + if let YamlNodeData::Scalar { value, .. } = &(*node).data { + yaml_free(*value as *mut libc::c_void); } } - if node.type_ == YAML_SEQUENCE_NODE { - STACK_DEL!(node.data.sequence.items); + if let YamlNodeData::Sequence { ref mut items, .. } = (*node).data { + STACK_DEL!(*items); } - if node.type_ == YAML_MAPPING_NODE { - STACK_DEL!(node.data.mapping.pairs); + if let YamlNodeData::Mapping { ref mut pairs, .. } = (*node).data { + STACK_DEL!(*pairs); } index += 1; } @@ -178,17 +177,17 @@ unsafe fn yaml_emitter_anchor_node(emitter: &mut yaml_emitter_t, index: libc::c_ addr_of_mut!((*(emitter.anchors).wrapping_offset((index - 1) as isize)).references); *fresh8 += 1; if (*emitter.anchors.wrapping_offset((index - 1) as isize)).references == 1 { - match (*node).type_ { - YAML_SEQUENCE_NODE => { - item = (*node).data.sequence.items.start; - while item < (*node).data.sequence.items.top { + match &(*node).data { + YamlNodeData::Sequence { items, .. } => { + item = items.start; + while item < items.top { yaml_emitter_anchor_node_sub(emitter, *item); item = item.wrapping_offset(1); } } - YAML_MAPPING_NODE => { - pair = (*node).data.mapping.pairs.start; - while pair < (*node).data.mapping.pairs.top { + YamlNodeData::Mapping { pairs, .. } => { + pair = pairs.start; + while pair < pairs.top { yaml_emitter_anchor_node_sub(emitter, (*pair).key); yaml_emitter_anchor_node_sub(emitter, (*pair).value); pair = pair.wrapping_offset(1); @@ -197,9 +196,8 @@ unsafe fn yaml_emitter_anchor_node(emitter: &mut yaml_emitter_t, index: libc::c_ _ => {} } } else if (*emitter.anchors.wrapping_offset((index - 1) as isize)).references == 2 { - let fresh9 = &mut emitter.last_anchor_id; - *fresh9 += 1; - (*emitter.anchors.wrapping_offset((index - 1) as isize)).anchor = *fresh9; + emitter.last_anchor_id += 1; + (*emitter.anchors.wrapping_offset((index - 1) as isize)).anchor = emitter.last_anchor_id; } } @@ -230,10 +228,10 @@ unsafe fn yaml_emitter_dump_node( return yaml_emitter_dump_alias(emitter, anchor); } (*emitter.anchors.wrapping_offset((index - 1) as isize)).serialized = true; - match (*node).type_ { - YAML_SCALAR_NODE => yaml_emitter_dump_scalar(emitter, node, anchor), - YAML_SEQUENCE_NODE => yaml_emitter_dump_sequence(emitter, node, anchor), - YAML_MAPPING_NODE => yaml_emitter_dump_mapping(emitter, node, anchor), + match (*node).data { + YamlNodeData::Scalar { .. } => yaml_emitter_dump_scalar(emitter, node, anchor), + YamlNodeData::Sequence { .. } => yaml_emitter_dump_sequence(emitter, node, anchor), + YamlNodeData::Mapping { .. } => yaml_emitter_dump_mapping(emitter, node, anchor), _ => __assert!(false), } } @@ -251,7 +249,7 @@ unsafe fn yaml_emitter_dump_alias( unsafe fn yaml_emitter_dump_scalar( emitter: &mut yaml_emitter_t, - node: *mut yaml_node_t, + node: *mut yaml_node_t, // TODO: take by value anchor: *mut yaml_char_t, ) -> Result<(), ()> { let plain_implicit = strcmp( @@ -263,20 +261,28 @@ unsafe fn yaml_emitter_dump_scalar( b"tag:yaml.org,2002:str\0" as *const u8 as *const libc::c_char, ) == 0; - let event = yaml_event_t { - data: YamlEventData::Scalar { - anchor, - tag: (*node).tag, - value: (*node).data.scalar.value, - length: (*node).data.scalar.length, - plain_implicit, - quoted_implicit, - style: (*node).data.scalar.style, - }, - ..Default::default() - }; - - yaml_emitter_emit(emitter, &event) + if let YamlNodeData::Scalar { + value, + length, + style, + } = &(*node).data + { + let event = yaml_event_t { + data: YamlEventData::Scalar { + anchor, + tag: (*node).tag, + value: *value, + length: *length, + plain_implicit, + quoted_implicit, + style: *style, + }, + ..Default::default() + }; + yaml_emitter_emit(emitter, &event) + } else { + unreachable!() + } } unsafe fn yaml_emitter_dump_sequence( @@ -290,27 +296,31 @@ unsafe fn yaml_emitter_dump_sequence( ) == 0; let mut item: *mut yaml_node_item_t; - let event = yaml_event_t { - data: YamlEventData::SequenceStart { - anchor, - tag: (*node).tag, - implicit, - style: (*node).data.sequence.style, - }, - ..Default::default() - }; + if let YamlNodeData::Sequence { items, style } = &(*node).data { + let event = yaml_event_t { + data: YamlEventData::SequenceStart { + anchor, + tag: (*node).tag, + implicit, + style: *style, + }, + ..Default::default() + }; - yaml_emitter_emit(emitter, &event)?; - item = (*node).data.sequence.items.start; - while item < (*node).data.sequence.items.top { - yaml_emitter_dump_node(emitter, *item)?; - item = item.wrapping_offset(1); + yaml_emitter_emit(emitter, &event)?; + item = items.start; + while item < items.top { + yaml_emitter_dump_node(emitter, *item)?; + item = item.wrapping_offset(1); + } + let event = yaml_event_t { + data: YamlEventData::SequenceEnd, + ..Default::default() + }; + yaml_emitter_emit(emitter, &event) + } else { + unreachable!() } - let event = yaml_event_t { - data: YamlEventData::SequenceEnd, - ..Default::default() - }; - yaml_emitter_emit(emitter, &event) } unsafe fn yaml_emitter_dump_mapping( @@ -324,26 +334,30 @@ unsafe fn yaml_emitter_dump_mapping( ) == 0; let mut pair: *mut yaml_node_pair_t; - let event = yaml_event_t { - data: YamlEventData::MappingStart { - anchor, - tag: (*node).tag, - implicit, - style: (*node).data.mapping.style, - }, - ..Default::default() - }; + if let YamlNodeData::Mapping { pairs, style } = &(*node).data { + let event = yaml_event_t { + data: YamlEventData::MappingStart { + anchor, + tag: (*node).tag, + implicit, + style: *style, + }, + ..Default::default() + }; - yaml_emitter_emit(emitter, &event)?; - pair = (*node).data.mapping.pairs.start; - while pair < (*node).data.mapping.pairs.top { - yaml_emitter_dump_node(emitter, (*pair).key)?; - yaml_emitter_dump_node(emitter, (*pair).value)?; - pair = pair.wrapping_offset(1); + yaml_emitter_emit(emitter, &event)?; + pair = pairs.start; + while pair < pairs.top { + yaml_emitter_dump_node(emitter, (*pair).key)?; + yaml_emitter_dump_node(emitter, (*pair).value)?; + pair = pair.wrapping_offset(1); + } + let event = yaml_event_t { + data: YamlEventData::MappingEnd, + ..Default::default() + }; + yaml_emitter_emit(emitter, &event) + } else { + unreachable!() } - let event = yaml_event_t { - data: YamlEventData::MappingEnd, - ..Default::default() - }; - yaml_emitter_emit(emitter, &event) } diff --git a/src/lib.rs b/src/lib.rs index c541e01..2941da6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -310,14 +310,14 @@ pub use crate::writer::yaml_emitter_flush; pub use crate::yaml::{ yaml_alias_data_t, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t, yaml_encoding_t, yaml_error_type_t, yaml_event_t, yaml_mapping_style_t, yaml_mark_t, - yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_node_type_t, yaml_parser_state_t, - yaml_parser_t, yaml_read_handler_t, yaml_scalar_style_t, yaml_sequence_style_t, - yaml_simple_key_t, yaml_stack_t, yaml_tag_directive_t, yaml_token_t, yaml_token_type_t, - yaml_version_directive_t, yaml_write_handler_t, YamlEventData, + yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_parser_state_t, yaml_parser_t, + yaml_read_handler_t, yaml_scalar_style_t, yaml_sequence_style_t, yaml_simple_key_t, + yaml_stack_t, yaml_tag_directive_t, yaml_token_t, yaml_token_type_t, yaml_version_directive_t, + yaml_write_handler_t, YamlEventData, }; #[doc(hidden)] pub use crate::yaml::{ yaml_break_t::*, yaml_emitter_state_t::*, yaml_encoding_t::*, yaml_error_type_t::*, - yaml_mapping_style_t::*, yaml_node_type_t::*, yaml_parser_state_t::*, yaml_scalar_style_t::*, + yaml_mapping_style_t::*, yaml_parser_state_t::*, yaml_scalar_style_t::*, yaml_sequence_style_t::*, yaml_token_type_t::*, }; diff --git a/src/loader.rs b/src/loader.rs index b7369b4..983402d 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,11 +1,10 @@ use crate::api::{yaml_free, yaml_malloc, yaml_stack_extend, yaml_strdup}; -use crate::externs::{memset, strcmp}; -use crate::yaml::{yaml_char_t, YamlEventData}; +use crate::externs::strcmp; +use crate::yaml::{yaml_char_t, YamlEventData, YamlNodeData}; use crate::{ libc, yaml_alias_data_t, yaml_document_delete, yaml_document_t, yaml_event_t, yaml_mark_t, yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_parser_parse, yaml_parser_t, PointerExt, - YAML_COMPOSER_ERROR, YAML_MAPPING_NODE, YAML_MEMORY_ERROR, YAML_SCALAR_NODE, - YAML_SEQUENCE_NODE, + YAML_COMPOSER_ERROR, YAML_MEMORY_ERROR, }; use core::mem::{size_of, MaybeUninit}; use core::ptr::{self, addr_of_mut}; @@ -239,17 +238,16 @@ unsafe fn yaml_parser_load_node_add( *((*parser.document).nodes.start).wrapping_offset((parent_index - 1) as isize) ); let current_block_17: u64; - match (*parent).type_ { - YAML_SEQUENCE_NODE => { - STACK_LIMIT!(parser, (*parent).data.sequence.items)?; - PUSH!((*parent).data.sequence.items, index); + match (*parent).data { + YamlNodeData::Sequence { ref mut items, .. } => { + STACK_LIMIT!(parser, items)?; + PUSH!(items, index); } - YAML_MAPPING_NODE => { + YamlNodeData::Mapping { ref mut pairs, .. } => { let mut pair = MaybeUninit::::uninit(); let pair = pair.as_mut_ptr(); - if !STACK_EMPTY!((*parent).data.mapping.pairs) { - let p: *mut yaml_node_pair_t = - (*parent).data.mapping.pairs.top.wrapping_offset(-1_isize); + if !STACK_EMPTY!(pairs) { + let p: *mut yaml_node_pair_t = pairs.top.wrapping_offset(-1_isize); if (*p).key != 0 && (*p).value == 0 { (*p).value = index; current_block_17 = 11307063007268554308; @@ -264,8 +262,8 @@ unsafe fn yaml_parser_load_node_add( _ => { (*pair).key = index; (*pair).value = 0; - STACK_LIMIT!(parser, (*parent).data.mapping.pairs)?; - PUSH!((*parent).data.mapping.pairs, *pair); + STACK_LIMIT!(parser, pairs)?; + PUSH!(pairs, *pair); } } } @@ -324,8 +322,6 @@ unsafe fn yaml_parser_load_scalar( }; let current_block: u64; - let mut node = MaybeUninit::::uninit(); - let node = node.as_mut_ptr(); let index: libc::c_int; if let Ok(()) = STACK_LIMIT!(parser, (*parser.document).nodes) { if tag.is_null() @@ -347,19 +343,17 @@ unsafe fn yaml_parser_load_scalar( current_block = 11006700562992250127; } if current_block != 10579931339944277179 { - memset( - node as *mut libc::c_void, - 0, - size_of::() as libc::c_ulong, - ); - (*node).type_ = YAML_SCALAR_NODE; - (*node).tag = tag; - (*node).start_mark = (*event).start_mark; - (*node).end_mark = (*event).end_mark; - (*node).data.scalar.value = value; - (*node).data.scalar.length = length; - (*node).data.scalar.style = style; - PUSH!((*parser.document).nodes, *node); + let node = yaml_node_t { + data: YamlNodeData::Scalar { + value, + length, + style, + }, + tag, + start_mark: (*event).start_mark, + end_mark: (*event).end_mark, + }; + PUSH!((*parser.document).nodes, node); index = (*parser.document) .nodes .top @@ -389,8 +383,6 @@ unsafe fn yaml_parser_load_sequence( }; let current_block: u64; - let mut node = MaybeUninit::::uninit(); - let node = node.as_mut_ptr(); struct Items { start: *mut yaml_node_item_t, end: *mut yaml_node_item_t, @@ -424,20 +416,22 @@ unsafe fn yaml_parser_load_sequence( } if current_block != 13474536459355229096 { STACK_INIT!(items, yaml_node_item_t); - memset( - node as *mut libc::c_void, - 0, - size_of::() as libc::c_ulong, - ); - (*node).type_ = YAML_SEQUENCE_NODE; - (*node).tag = tag; - (*node).start_mark = (*event).start_mark; - (*node).end_mark = (*event).end_mark; - (*node).data.sequence.items.start = items.start; - (*node).data.sequence.items.end = items.end; - (*node).data.sequence.items.top = items.start; - (*node).data.sequence.style = style; - PUSH!((*parser.document).nodes, *node); + + let node = yaml_node_t { + data: YamlNodeData::Sequence { + items: crate::yaml_stack_t { + start: items.start, + end: items.end, + top: items.start, + }, + style, + }, + tag, + start_mark: (*event).start_mark, + end_mark: (*event).end_mark, + }; + + PUSH!((*parser.document).nodes, node); index = (*parser.document) .nodes .top @@ -461,10 +455,10 @@ unsafe fn yaml_parser_load_sequence_end( ) -> Result<(), ()> { __assert!(((*ctx).top).c_offset_from((*ctx).start) as libc::c_long > 0_i64); let index: libc::c_int = *(*ctx).top.wrapping_offset(-1_isize); - __assert!( - (*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).type_ - == YAML_SEQUENCE_NODE - ); + __assert!(matches!( + (*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).data, + YamlNodeData::Sequence { .. } + )); (*(*parser.document) .nodes .start @@ -489,8 +483,6 @@ unsafe fn yaml_parser_load_mapping( }; let current_block: u64; - let mut node = MaybeUninit::::uninit(); - let node = node.as_mut_ptr(); struct Pairs { start: *mut yaml_node_pair_t, end: *mut yaml_node_pair_t, @@ -524,20 +516,20 @@ unsafe fn yaml_parser_load_mapping( } if current_block != 13635467803606088781 { STACK_INIT!(pairs, yaml_node_pair_t); - memset( - node as *mut libc::c_void, - 0, - size_of::() as libc::c_ulong, - ); - (*node).type_ = YAML_MAPPING_NODE; - (*node).tag = tag; - (*node).start_mark = (*event).start_mark; - (*node).end_mark = (*event).end_mark; - (*node).data.mapping.pairs.start = pairs.start; - (*node).data.mapping.pairs.end = pairs.end; - (*node).data.mapping.pairs.top = pairs.start; - (*node).data.mapping.style = style; - PUSH!((*parser.document).nodes, *node); + let node = yaml_node_t { + data: YamlNodeData::Mapping { + pairs: crate::yaml_stack_t { + start: pairs.start, + end: pairs.end, + top: pairs.start, + }, + style, + }, + tag, + start_mark: (*event).start_mark, + end_mark: (*event).end_mark, + }; + PUSH!((*parser.document).nodes, node); index = (*parser.document) .nodes .top @@ -561,10 +553,10 @@ unsafe fn yaml_parser_load_mapping_end( ) -> Result<(), ()> { __assert!(((*ctx).top).c_offset_from((*ctx).start) as libc::c_long > 0_i64); let index: libc::c_int = *(*ctx).top.wrapping_offset(-1_isize); - __assert!( - (*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).type_ - == YAML_MAPPING_NODE - ); + __assert!(matches!( + (*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).data, + YamlNodeData::Mapping { .. } + )); (*(*parser.document) .nodes .start diff --git a/src/macros.rs b/src/macros.rs index 5e5cf61..bd92038 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -442,12 +442,10 @@ macro_rules! PUSH { } macro_rules! POP { - ($stack:expr) => { - *{ - $stack.top = $stack.top.offset(-1); - $stack.top - } - }; + ($stack:expr) => {{ + $stack.top = $stack.top.offset(-1); + core::ptr::read($stack.top) + }}; } macro_rules! QUEUE_INIT { diff --git a/src/yaml.rs b/src/yaml.rs index b7e6b6b..a2af297 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -2,7 +2,7 @@ use crate::libc; use core::ops::Deref; use core::ptr::{self, addr_of}; -pub use self::{yaml_encoding_t::*, yaml_node_type_t::*}; +pub use self::yaml_encoding_t::*; pub use core::primitive::{i64 as ptrdiff_t, u64 as size_t, u8 as yaml_char_t}; /// The version directive data. @@ -474,114 +474,53 @@ pub enum YamlEventData { MappingEnd, } -/// Node types. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[repr(u32)] -#[non_exhaustive] -pub enum yaml_node_type_t { - /// An empty node. - YAML_NO_NODE = 0, - /// A scalar node. - YAML_SCALAR_NODE = 1, - /// A sequence node. - YAML_SEQUENCE_NODE = 2, - /// A mapping node. - YAML_MAPPING_NODE = 3, -} - /// The node structure. -#[derive(Copy, Clone)] #[repr(C)] #[non_exhaustive] pub struct yaml_node_t { /// The node type. - pub type_: yaml_node_type_t, + pub data: YamlNodeData, /// The node tag. pub tag: *mut yaml_char_t, - /// The node data. - /// - /// ``` - /// # const _: &str = stringify! { - /// union { - /// /// The scalar parameters (for YAML_SCALAR_NODE). - /// scalar: struct { - /// /// The scalar value. - /// value: *mut u8, - /// /// The length of the scalar value. - /// length: u64, - /// /// The scalar style. - /// style: yaml_scalar_style_t, - /// }, - /// /// The sequence parameters (for YAML_SEQUENCE_NODE). - /// sequence: struct { - /// /// The stack of sequence items. - /// items: yaml_stack_t, - /// /// The sequence style. - /// style: yaml_sequence_style_t, - /// }, - /// /// The mapping parameters (for YAML_MAPPING_NODE). - /// mapping: struct { - /// /// The stack of mapping pairs (key, value). - /// pairs: yaml_stack_t, - /// /// The mapping style. - /// style: yaml_mapping_style_t, - /// }, - /// } - /// # }; - /// ``` - pub data: unnamed_yaml_node_t_data, /// The beginning of the node. pub start_mark: yaml_mark_t, /// The end of the node. pub end_mark: yaml_mark_t, } -#[derive(Copy, Clone)] -#[repr(C)] -pub union unnamed_yaml_node_t_data { - /// The scalar parameters (for YAML_SCALAR_NODE). - pub scalar: unnamed_yaml_node_t_data_scalar, - /// The sequence parameters (for YAML_SEQUENCE_NODE). - pub sequence: unnamed_yaml_node_t_data_sequence, - /// The mapping parameters (for YAML_MAPPING_NODE). - pub mapping: unnamed_yaml_node_t_data_mapping, -} - -#[derive(Copy, Clone)] -#[repr(C)] -#[non_exhaustive] -pub struct unnamed_yaml_node_t_data_scalar { - /// The scalar value. - pub value: *mut yaml_char_t, - /// The length of the scalar value. - pub length: size_t, - /// The scalar style. - pub style: yaml_scalar_style_t, +/// Node types. +#[derive(Default)] +pub enum YamlNodeData { + /// An empty node. + #[default] + NoNode, + /// A scalar node. + Scalar { + /// The scalar value. + value: *mut yaml_char_t, + /// The length of the scalar value. + length: size_t, + /// The scalar style. + style: yaml_scalar_style_t, + }, + /// A sequence node. + Sequence { + /// The stack of sequence items. + items: yaml_stack_t, + /// The sequence style. + style: yaml_sequence_style_t, + }, + /// A mapping node. + Mapping { + /// The stack of mapping pairs (key, value). + pairs: yaml_stack_t, + /// The mapping style. + style: yaml_mapping_style_t, + }, } /// An element of a sequence node. pub type yaml_node_item_t = libc::c_int; - -#[derive(Copy, Clone)] -#[repr(C)] -#[non_exhaustive] -pub struct unnamed_yaml_node_t_data_sequence { - /// The stack of sequence items. - pub items: yaml_stack_t, - /// The sequence style. - pub style: yaml_sequence_style_t, -} - -#[derive(Copy, Clone)] -#[repr(C)] -#[non_exhaustive] -pub struct unnamed_yaml_node_t_data_mapping { - /// The stack of mapping pairs (key, value). - pub pairs: yaml_stack_t, - /// The mapping style. - pub style: yaml_mapping_style_t, -} - /// An element of a mapping node. #[derive(Copy, Clone)] #[repr(C)]