From afe79b057349b14c0414608ffe529eaa54594ed2 Mon Sep 17 00:00:00 2001 From: Alex Rocha Date: Tue, 13 Jan 2026 11:21:41 -0800 Subject: [PATCH] Use NonNull wrapper for parser pointers --- rust/ruby-rbs/build.rs | 4 ++-- rust/ruby-rbs/src/lib.rs | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/rust/ruby-rbs/build.rs b/rust/ruby-rbs/build.rs index bd0d6d2ca..d0f753cae 100644 --- a/rust/ruby-rbs/build.rs +++ b/rust/ruby-rbs/build.rs @@ -339,7 +339,7 @@ fn generate(config: &Config) -> Result<(), Box> { writeln!(file, "#[allow(dead_code)]")?; // TODO: Remove this once all nodes that need parser are implemented writeln!(file, "#[derive(Debug)]")?; writeln!(file, "pub struct {}<'a> {{", node.rust_name)?; - writeln!(file, " parser: *mut rbs_parser_t,")?; + writeln!(file, " parser: NonNull,")?; writeln!( file, " pointer: *mut {},", @@ -525,7 +525,7 @@ fn generate(config: &Config) -> Result<(), Box> { writeln!(file, " #[allow(clippy::missing_safety_doc)]")?; writeln!( file, - " fn new(parser: *mut rbs_parser_t, node: *mut rbs_node_t) -> Self {{" + " fn new(parser: NonNull, node: *mut rbs_node_t) -> Self {{" )?; writeln!(file, " match unsafe {{ (*node).type_ }} {{")?; for node in &config.nodes { diff --git a/rust/ruby-rbs/src/lib.rs b/rust/ruby-rbs/src/lib.rs index 1ffa89513..7dcae85f4 100644 --- a/rust/ruby-rbs/src/lib.rs +++ b/rust/ruby-rbs/src/lib.rs @@ -2,6 +2,7 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs")); use rbs_encoding_type_t::RBS_ENCODING_UTF_8; use ruby_rbs_sys::bindings::*; use std::marker::PhantomData; +use std::ptr::NonNull; use std::sync::Once; static INIT: Once = Once::new(); @@ -32,7 +33,7 @@ pub fn parse(rbs_code: &[u8]) -> Result, String> { let result = rbs_parse_signature(parser, &mut signature); let signature_node = SignatureNode { - parser, + parser: NonNull::new_unchecked(parser), pointer: signature, marker: PhantomData, }; @@ -48,7 +49,7 @@ pub fn parse(rbs_code: &[u8]) -> Result, String> { impl Drop for SignatureNode<'_> { fn drop(&mut self) { unsafe { - rbs_parser_free(self.parser); + rbs_parser_free(self.parser.as_ptr()); } } } @@ -57,7 +58,7 @@ impl KeywordNode<'_> { pub fn name(&self) -> &[u8] { unsafe { let constant_ptr = rbs_constant_pool_id_to_constant( - &(*self.parser).constant_pool, + &(*self.parser.as_ptr()).constant_pool, (*self.pointer).constant_id, ); if constant_ptr.is_null() { @@ -71,13 +72,13 @@ impl KeywordNode<'_> { } pub struct NodeList<'a> { - parser: *mut rbs_parser_t, + parser: NonNull, pointer: *mut rbs_node_list_t, marker: PhantomData<&'a mut rbs_node_list_t>, } impl<'a> NodeList<'a> { - pub fn new(parser: *mut rbs_parser_t, pointer: *mut rbs_node_list_t) -> Self { + pub fn new(parser: NonNull, pointer: *mut rbs_node_list_t) -> Self { Self { parser, pointer, @@ -97,7 +98,7 @@ impl<'a> NodeList<'a> { } pub struct NodeListIter<'a> { - parser: *mut rbs_parser_t, + parser: NonNull, current: *mut rbs_node_list_node_t, marker: PhantomData<&'a mut rbs_node_list_node_t>, } @@ -118,13 +119,13 @@ impl<'a> Iterator for NodeListIter<'a> { } pub struct RBSHash<'a> { - parser: *mut rbs_parser_t, + parser: NonNull, pointer: *mut rbs_hash, marker: PhantomData<&'a mut rbs_hash>, } impl<'a> RBSHash<'a> { - pub fn new(parser: *mut rbs_parser_t, pointer: *mut rbs_hash) -> Self { + pub fn new(parser: NonNull, pointer: *mut rbs_hash) -> Self { Self { parser, pointer, @@ -144,7 +145,7 @@ impl<'a> RBSHash<'a> { } pub struct RBSHashIter<'a> { - parser: *mut rbs_parser_t, + parser: NonNull, current: *mut rbs_hash_node_t, marker: PhantomData<&'a mut rbs_hash_node_t>, } @@ -242,7 +243,7 @@ impl SymbolNode<'_> { pub fn name(&self) -> &[u8] { unsafe { let constant_ptr = rbs_constant_pool_id_to_constant( - &(*self.parser).constant_pool, + &(*self.parser.as_ptr()).constant_pool, (*self.pointer).constant_id, ); if constant_ptr.is_null() {