Skip to content

Commit c45afcd

Browse files
committed
Use NonNull wrapper for parser pointers
1 parent 33d9ae5 commit c45afcd

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

rust/ruby-rbs/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
339339
writeln!(file, "#[allow(dead_code)]")?; // TODO: Remove this once all nodes that need parser are implemented
340340
writeln!(file, "#[derive(Debug)]")?;
341341
writeln!(file, "pub struct {}<'a> {{", node.rust_name)?;
342-
writeln!(file, " parser: *mut rbs_parser_t,")?;
342+
writeln!(file, " parser: NonNull<rbs_parser_t>,")?;
343343
writeln!(
344344
file,
345345
" pointer: *mut {},",
@@ -525,7 +525,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
525525
writeln!(file, " #[allow(clippy::missing_safety_doc)]")?;
526526
writeln!(
527527
file,
528-
" fn new(parser: *mut rbs_parser_t, node: *mut rbs_node_t) -> Self {{"
528+
" fn new(parser: NonNull<rbs_parser_t>, node: *mut rbs_node_t) -> Self {{"
529529
)?;
530530
writeln!(file, " match unsafe {{ (*node).type_ }} {{")?;
531531
for node in &config.nodes {

rust/ruby-rbs/src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
22
use rbs_encoding_type_t::RBS_ENCODING_UTF_8;
33
use ruby_rbs_sys::bindings::*;
44
use std::marker::PhantomData;
5+
use std::ptr::NonNull;
56
use std::sync::Once;
67

78
static INIT: Once = Once::new();
@@ -32,7 +33,7 @@ pub fn parse(rbs_code: &[u8]) -> Result<SignatureNode<'_>, String> {
3233
let result = rbs_parse_signature(parser, &mut signature);
3334

3435
let signature_node = SignatureNode {
35-
parser,
36+
parser: NonNull::new_unchecked(parser),
3637
pointer: signature,
3738
marker: PhantomData,
3839
};
@@ -48,7 +49,7 @@ pub fn parse(rbs_code: &[u8]) -> Result<SignatureNode<'_>, String> {
4849
impl Drop for SignatureNode<'_> {
4950
fn drop(&mut self) {
5051
unsafe {
51-
rbs_parser_free(self.parser);
52+
rbs_parser_free(self.parser.as_ptr());
5253
}
5354
}
5455
}
@@ -57,7 +58,7 @@ impl KeywordNode<'_> {
5758
pub fn name(&self) -> &[u8] {
5859
unsafe {
5960
let constant_ptr = rbs_constant_pool_id_to_constant(
60-
&(*self.parser).constant_pool,
61+
&(*self.parser.as_ptr()).constant_pool,
6162
(*self.pointer).constant_id,
6263
);
6364
if constant_ptr.is_null() {
@@ -71,13 +72,13 @@ impl KeywordNode<'_> {
7172
}
7273

7374
pub struct NodeList<'a> {
74-
parser: *mut rbs_parser_t,
75+
parser: NonNull<rbs_parser_t>,
7576
pointer: *mut rbs_node_list_t,
7677
marker: PhantomData<&'a mut rbs_node_list_t>,
7778
}
7879

7980
impl<'a> NodeList<'a> {
80-
pub fn new(parser: *mut rbs_parser_t, pointer: *mut rbs_node_list_t) -> Self {
81+
pub fn new(parser: NonNull<rbs_parser_t>, pointer: *mut rbs_node_list_t) -> Self {
8182
Self {
8283
parser,
8384
pointer,
@@ -97,7 +98,7 @@ impl<'a> NodeList<'a> {
9798
}
9899

99100
pub struct NodeListIter<'a> {
100-
parser: *mut rbs_parser_t,
101+
parser: NonNull<rbs_parser_t>,
101102
current: *mut rbs_node_list_node_t,
102103
marker: PhantomData<&'a mut rbs_node_list_node_t>,
103104
}
@@ -118,13 +119,13 @@ impl<'a> Iterator for NodeListIter<'a> {
118119
}
119120

120121
pub struct RBSHash<'a> {
121-
parser: *mut rbs_parser_t,
122+
parser: NonNull<rbs_parser_t>,
122123
pointer: *mut rbs_hash,
123124
marker: PhantomData<&'a mut rbs_hash>,
124125
}
125126

126127
impl<'a> RBSHash<'a> {
127-
pub fn new(parser: *mut rbs_parser_t, pointer: *mut rbs_hash) -> Self {
128+
pub fn new(parser: NonNull<rbs_parser_t>, pointer: *mut rbs_hash) -> Self {
128129
Self {
129130
parser,
130131
pointer,
@@ -144,7 +145,7 @@ impl<'a> RBSHash<'a> {
144145
}
145146

146147
pub struct RBSHashIter<'a> {
147-
parser: *mut rbs_parser_t,
148+
parser: NonNull<rbs_parser_t>,
148149
current: *mut rbs_hash_node_t,
149150
marker: PhantomData<&'a mut rbs_hash_node_t>,
150151
}
@@ -242,7 +243,7 @@ impl SymbolNode<'_> {
242243
pub fn name(&self) -> &[u8] {
243244
unsafe {
244245
let constant_ptr = rbs_constant_pool_id_to_constant(
245-
&(*self.parser).constant_pool,
246+
&(*self.parser.as_ptr()).constant_pool,
246247
(*self.pointer).constant_id,
247248
);
248249
if constant_ptr.is_null() {

0 commit comments

Comments
 (0)