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: 9 additions & 0 deletions rust/ruby-rbs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
)?;
writeln!(file, " }}")?;
}
"rbs_keyword" => {
writeln!(file, " pub fn {}(&self) -> RBSKeyword {{", field.name)?;
writeln!(
file,
" RBSKeyword::new(self.parser, unsafe {{ (*self.pointer).{} }})",
field.name
)?;
writeln!(file, " }}")?;
}
_ => eprintln!("Unknown field type: {}", field.c_type),
}
}
Expand Down
26 changes: 26 additions & 0 deletions rust/ruby-rbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ impl RBSSymbol {
}
}

pub struct RBSKeyword {
parser: *mut rbs_parser_t,
pointer: *const rbs_keyword,
}

impl RBSKeyword {
pub fn new(parser: *mut rbs_parser_t, pointer: *const rbs_keyword) -> Self {
Self { parser, pointer }
}

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