diff --git a/rust/ruby-rbs/build.rs b/rust/ruby-rbs/build.rs index 517bc37dd..5eb249233 100644 --- a/rust/ruby-rbs/build.rs +++ b/rust/ruby-rbs/build.rs @@ -143,7 +143,9 @@ fn write_node_field_accessor( field.c_name() )?; } - writeln!(file, " }}") + writeln!(file, " }}")?; + writeln!(file)?; + Ok(()) } fn write_visit_trait(file: &mut File, config: &Config) -> Result<(), Box> { @@ -369,11 +371,13 @@ fn generate(config: &Config) -> Result<(), Box> { field.c_name() )?; writeln!(file, " }}")?; + writeln!(file)?; } "bool" => { writeln!(file, " pub fn {}(&self) -> bool {{", field.name)?; writeln!(file, " unsafe {{ (*self.pointer).{} }}", field.name)?; writeln!(file, " }}")?; + writeln!(file)?; } "rbs_ast_comment" => { write_node_field_accessor(&mut file, field, "CommentNode")? @@ -412,6 +416,7 @@ fn generate(config: &Config) -> Result<(), Box> { )?; writeln!(file, " }}")?; } + writeln!(file)?; } "rbs_location_list" => { if field.optional { @@ -444,6 +449,7 @@ fn generate(config: &Config) -> Result<(), Box> { )?; writeln!(file, " }}")?; } + writeln!(file)?; } "rbs_namespace" => { write_node_field_accessor(&mut file, field, "NamespaceNode")?; @@ -474,6 +480,7 @@ fn generate(config: &Config) -> Result<(), Box> { )?; } writeln!(file, " }}")?; + writeln!(file)?; } "rbs_node_list" => { write_node_field_accessor(&mut file, field, "NodeList")?; diff --git a/rust/ruby-rbs/src/lib.rs b/rust/ruby-rbs/src/lib.rs index 0dc116263..ba4130365 100644 --- a/rust/ruby-rbs/src/lib.rs +++ b/rust/ruby-rbs/src/lib.rs @@ -51,22 +51,19 @@ impl Drop for SignatureNode { } } -pub struct NodeListIter { - parser: *mut rbs_parser_t, - current: *mut rbs_node_list_node_t, -} - -impl Iterator for NodeListIter { - type Item = Node; +impl KeywordNode { + pub fn name(&self) -> &[u8] { + unsafe { + let constant_ptr = rbs_constant_pool_id_to_constant( + &(*self.parser).constant_pool, + (*self.pointer).constant_id, + ); + if constant_ptr.is_null() { + panic!("Constant ID for keyword is not present in the pool"); + } - fn next(&mut self) -> Option { - if self.current.is_null() { - None - } else { - let pointer_data = unsafe { *self.current }; - let node = Node::new(self.parser, pointer_data.node); - self.current = pointer_data.next; - Some(node) + let constant = &*constant_ptr; + std::slice::from_raw_parts(constant.start, constant.length) } } } @@ -91,6 +88,26 @@ impl NodeList { } } +pub struct NodeListIter { + parser: *mut rbs_parser_t, + current: *mut rbs_node_list_node_t, +} + +impl Iterator for NodeListIter { + type Item = Node; + + fn next(&mut self) -> Option { + if self.current.is_null() { + None + } else { + let pointer_data = unsafe { *self.current }; + let node = Node::new(self.parser, pointer_data.node); + self.current = pointer_data.next; + Some(node) + } + } +} + pub struct RBSHash { parser: *mut rbs_parser_t, pointer: *mut rbs_hash, @@ -150,6 +167,24 @@ impl RBSLocation { } } +pub struct RBSLocationList { + pointer: *mut rbs_location_list, +} + +impl RBSLocationList { + pub fn new(pointer: *mut rbs_location_list) -> Self { + Self { pointer } + } + + /// Returns an iterator over the locations. + #[must_use] + pub fn iter(&self) -> RBSLocationListIter { + RBSLocationListIter { + current: unsafe { (*self.pointer).head }, + } + } +} + pub struct RBSLocationListIter { current: *mut rbs_location_list_node_t, } @@ -169,24 +204,6 @@ impl Iterator for RBSLocationListIter { } } -pub struct RBSLocationList { - pointer: *mut rbs_location_list, -} - -impl RBSLocationList { - pub fn new(pointer: *mut rbs_location_list) -> Self { - Self { pointer } - } - - /// Returns an iterator over the locations. - #[must_use] - pub fn iter(&self) -> RBSLocationListIter { - RBSLocationListIter { - current: unsafe { (*self.pointer).head }, - } - } -} - #[derive(Debug)] pub struct RBSString { pointer: *const rbs_string_t, @@ -222,23 +239,6 @@ impl SymbolNode { } } -impl KeywordNode { - pub fn name(&self) -> &[u8] { - unsafe { - let constant_ptr = rbs_constant_pool_id_to_constant( - &(*self.parser).constant_pool, - (*self.pointer).constant_id, - ); - if constant_ptr.is_null() { - panic!("Constant ID for keyword is not present in the pool"); - } - - let constant = &*constant_ptr; - std::slice::from_raw_parts(constant.start, constant.length) - } - } -} - #[cfg(test)] mod tests { use super::*;