[librsvg: 2/20] Initialize the xmlSAXHandler in a more legible way



commit fa8874ac25480341c0a61a9c4a360ddc2a226048
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue May 28 17:05:07 2019 -0500

    Initialize the xmlSAXHandler in a more legible way

 rsvg_internals/src/xml2.rs      | 46 +++++++++++++++++++---------------
 rsvg_internals/src/xml2_load.rs | 55 ++++++++++++++++++++++++++++++-----------
 2 files changed, 66 insertions(+), 35 deletions(-)
---
diff --git a/rsvg_internals/src/xml2.rs b/rsvg_internals/src/xml2.rs
index a2afb7dd..79b30716 100644
--- a/rsvg_internals/src/xml2.rs
+++ b/rsvg_internals/src/xml2.rs
@@ -18,13 +18,17 @@ pub type xmlDocPtr = gpointer;
 
 pub type xmlEntityPtr = gpointer;
 
+pub type UnusedFn = Option<unsafe extern "C" fn()>;
+
+#[cfg_attr(rustfmt, rustfmt_skip)]
 #[repr(C)]
 pub struct xmlSAXHandler {
-    pub internalSubset: gpointer,
-    pub isStandalone: gpointer,
-    pub hasInternalSubset: gpointer,
-    pub hasExternalSubset: gpointer,
-    pub resolveEntity: gpointer,
+    pub internalSubset:    UnusedFn,
+    pub isStandalone:      UnusedFn,
+    pub hasInternalSubset: UnusedFn,
+    pub hasExternalSubset: UnusedFn,
+    pub resolveEntity:     UnusedFn,
+
     pub getEntity: Option<unsafe extern "C" fn(
         ctx: *mut libc::c_void,
         name: *const libc::c_char,
@@ -39,9 +43,9 @@ pub struct xmlSAXHandler {
         content: *const libc::c_char,
     )>,
 
-    pub notationDecl: gpointer,
-    pub attributeDecl: gpointer,
-    pub elementDecl: gpointer,
+    pub notationDecl:  UnusedFn,
+    pub attributeDecl: UnusedFn,
+    pub elementDecl:   UnusedFn,
 
     pub unparsedEntityDecl: Option<unsafe extern "C" fn(
         ctx: *mut libc::c_void,
@@ -51,9 +55,9 @@ pub struct xmlSAXHandler {
         notation_name: *const libc::c_char,
     )>,
 
-    pub setDocumentLocator: gpointer,
-    pub startDocument: gpointer,
-    pub endDocument: gpointer,
+    pub setDocumentLocator: UnusedFn,
+    pub startDocument:      UnusedFn,
+    pub endDocument:        UnusedFn,
 
     pub startElement: Option<unsafe extern "C" fn(
         ctx: *mut libc::c_void,
@@ -66,7 +70,7 @@ pub struct xmlSAXHandler {
         name: *const libc::c_char,
     )>,
 
-    pub reference: gpointer,
+    pub reference: UnusedFn,
 
     pub characters: Option<unsafe extern "C" fn(
         ctx: *mut libc::c_void,
@@ -74,7 +78,7 @@ pub struct xmlSAXHandler {
         len: libc::c_int,
     )>,
 
-    pub ignorableWhitespace: gpointer,
+    pub ignorableWhitespace: UnusedFn,
 
     pub processingInstruction: Option<unsafe extern "C" fn(
         ctx: *mut libc::c_void,
@@ -82,12 +86,12 @@ pub struct xmlSAXHandler {
         data: *const libc::c_char,
     )>,
 
-    pub comment: gpointer,
-    pub warning: gpointer,
+    pub comment:    UnusedFn,
+    pub warning:    UnusedFn,
 
-    pub error: gpointer,
+    pub error:      UnusedFn,
 
-    pub fatalError: gpointer,
+    pub fatalError: UnusedFn,
 
     pub getParameterEntity: Option<unsafe extern "C" fn(
         ctx: *mut libc::c_void,
@@ -100,13 +104,15 @@ pub struct xmlSAXHandler {
         len: libc::c_int,
     )>,
 
-    pub externalSubset: gpointer,
+    pub externalSubset: UnusedFn,
 
     pub initialized: libc::c_uint,
 
     pub _private: gpointer,
-    pub startElementNs: gpointer,
-    pub endElementNs: gpointer,
+
+    pub startElementNs: UnusedFn,
+    pub endElementNs:   UnusedFn,
+
     pub serror: Option<unsafe extern "C" fn(user_data: *mut libc::c_void, error: xmlErrorPtr)>,
 }
 
diff --git a/rsvg_internals/src/xml2_load.rs b/rsvg_internals/src/xml2_load.rs
index 97b15e50..7e50a33d 100644
--- a/rsvg_internals/src/xml2_load.rs
+++ b/rsvg_internals/src/xml2_load.rs
@@ -5,7 +5,6 @@ use gio;
 use gio::prelude::*;
 use std::borrow::Cow;
 use std::cell::{Cell, RefCell};
-use std::mem;
 use std::ptr;
 use std::rc::Rc;
 use std::slice;
@@ -21,21 +20,47 @@ use crate::util::utf8_cstr;
 use crate::xml::XmlState;
 use crate::xml2::*;
 
+#[cfg_attr(rustfmt, rustfmt_skip)]
 fn get_xml2_sax_handler() -> xmlSAXHandler {
-    let mut h: xmlSAXHandler = unsafe { mem::zeroed() };
-
-    h.getEntity = Some(sax_get_entity_cb);
-    h.entityDecl = Some(sax_entity_decl_cb);
-    h.unparsedEntityDecl = Some(sax_unparsed_entity_decl_cb);
-    h.getParameterEntity = Some(sax_get_parameter_entity_cb);
-    h.characters = Some(sax_characters_cb);
-    h.cdataBlock = Some(sax_characters_cb);
-    h.startElement = Some(sax_start_element_cb);
-    h.endElement = Some(sax_end_element_cb);
-    h.processingInstruction = Some(sax_processing_instruction_cb);
-    h.serror = Some(rsvg_sax_serror_cb);
-
-    h
+    xmlSAXHandler {
+        // first the unused callbacks
+        internalSubset:        None,
+        isStandalone:          None,
+        hasInternalSubset:     None,
+        hasExternalSubset:     None,
+        resolveEntity:         None,
+        notationDecl:          None,
+        attributeDecl:         None,
+        elementDecl:           None,
+        setDocumentLocator:    None,
+        startDocument:         None,
+        endDocument:           None,
+        reference:             None,
+        ignorableWhitespace:   None,
+        comment:               None,
+        warning:               None,
+        error:                 None,
+        fatalError:            None,
+        externalSubset:        None,
+        startElementNs:        None,
+        endElementNs:          None,
+
+        _private:              ptr::null_mut(),
+
+        // then the used callbacks
+        getEntity:             Some(sax_get_entity_cb),
+        entityDecl:            Some(sax_entity_decl_cb),
+        unparsedEntityDecl:    Some(sax_unparsed_entity_decl_cb),
+        getParameterEntity:    Some(sax_get_parameter_entity_cb),
+        characters:            Some(sax_characters_cb),
+        cdataBlock:            Some(sax_characters_cb),
+        startElement:          Some(sax_start_element_cb),
+        endElement:            Some(sax_end_element_cb),
+        processingInstruction: Some(sax_processing_instruction_cb),
+        serror:                Some(rsvg_sax_serror_cb),
+
+        initialized:           0,
+    }
 }
 
 unsafe extern "C" fn rsvg_sax_serror_cb(user_data: *mut libc::c_void, error: xmlErrorPtr) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]