Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion rust/ruby-rbs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand Down Expand Up @@ -369,11 +371,13 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
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")?
Expand Down Expand Up @@ -412,6 +416,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
)?;
writeln!(file, " }}")?;
}
writeln!(file)?;
}
"rbs_location_list" => {
if field.optional {
Expand Down Expand Up @@ -444,6 +449,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
)?;
writeln!(file, " }}")?;
}
writeln!(file)?;
}
"rbs_namespace" => {
write_node_field_accessor(&mut file, field, "NamespaceNode")?;
Expand Down Expand Up @@ -474,6 +480,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
)?;
}
writeln!(file, " }}")?;
writeln!(file)?;
}
"rbs_node_list" => {
write_node_field_accessor(&mut file, field, "NodeList")?;
Expand Down
100 changes: 50 additions & 50 deletions rust/ruby-rbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::Item> {
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)
}
}
}
Expand All @@ -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<Self::Item> {
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,
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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::*;
Expand Down
Loading