Skip to content

Commit 0392b63

Browse files
committed
Small polish pass (#77)
Reorder lib.rs structs alphabetically Improve bindings code formatting
1 parent 97e8cbd commit 0392b63

File tree

2 files changed

+58
-51
lines changed

2 files changed

+58
-51
lines changed

rust/ruby-rbs/build.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ fn write_node_field_accessor(
143143
field.c_name()
144144
)?;
145145
}
146-
writeln!(file, " }}")
146+
writeln!(file, " }}")?;
147+
writeln!(file)?;
148+
Ok(())
147149
}
148150

149151
fn write_visit_trait(file: &mut File, config: &Config) -> Result<(), Box<dyn std::error::Error>> {
@@ -369,11 +371,13 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
369371
field.c_name()
370372
)?;
371373
writeln!(file, " }}")?;
374+
writeln!(file)?;
372375
}
373376
"bool" => {
374377
writeln!(file, " pub fn {}(&self) -> bool {{", field.name)?;
375378
writeln!(file, " unsafe {{ (*self.pointer).{} }}", field.name)?;
376379
writeln!(file, " }}")?;
380+
writeln!(file)?;
377381
}
378382
"rbs_ast_comment" => {
379383
write_node_field_accessor(&mut file, field, "CommentNode")?
@@ -412,6 +416,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
412416
)?;
413417
writeln!(file, " }}")?;
414418
}
419+
writeln!(file)?;
415420
}
416421
"rbs_location_list" => {
417422
if field.optional {
@@ -444,6 +449,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
444449
)?;
445450
writeln!(file, " }}")?;
446451
}
452+
writeln!(file)?;
447453
}
448454
"rbs_namespace" => {
449455
write_node_field_accessor(&mut file, field, "NamespaceNode")?;
@@ -474,6 +480,7 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
474480
)?;
475481
}
476482
writeln!(file, " }}")?;
483+
writeln!(file)?;
477484
}
478485
"rbs_node_list" => {
479486
write_node_field_accessor(&mut file, field, "NodeList")?;

rust/ruby-rbs/src/lib.rs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,19 @@ impl Drop for SignatureNode {
5151
}
5252
}
5353

54-
pub struct NodeListIter {
55-
parser: *mut rbs_parser_t,
56-
current: *mut rbs_node_list_node_t,
57-
}
58-
59-
impl Iterator for NodeListIter {
60-
type Item = Node;
54+
impl KeywordNode {
55+
pub fn name(&self) -> &[u8] {
56+
unsafe {
57+
let constant_ptr = rbs_constant_pool_id_to_constant(
58+
&(*self.parser).constant_pool,
59+
(*self.pointer).constant_id,
60+
);
61+
if constant_ptr.is_null() {
62+
panic!("Constant ID for keyword is not present in the pool");
63+
}
6164

62-
fn next(&mut self) -> Option<Self::Item> {
63-
if self.current.is_null() {
64-
None
65-
} else {
66-
let pointer_data = unsafe { *self.current };
67-
let node = Node::new(self.parser, pointer_data.node);
68-
self.current = pointer_data.next;
69-
Some(node)
65+
let constant = &*constant_ptr;
66+
std::slice::from_raw_parts(constant.start, constant.length)
7067
}
7168
}
7269
}
@@ -91,6 +88,26 @@ impl NodeList {
9188
}
9289
}
9390

91+
pub struct NodeListIter {
92+
parser: *mut rbs_parser_t,
93+
current: *mut rbs_node_list_node_t,
94+
}
95+
96+
impl Iterator for NodeListIter {
97+
type Item = Node;
98+
99+
fn next(&mut self) -> Option<Self::Item> {
100+
if self.current.is_null() {
101+
None
102+
} else {
103+
let pointer_data = unsafe { *self.current };
104+
let node = Node::new(self.parser, pointer_data.node);
105+
self.current = pointer_data.next;
106+
Some(node)
107+
}
108+
}
109+
}
110+
94111
pub struct RBSHash {
95112
parser: *mut rbs_parser_t,
96113
pointer: *mut rbs_hash,
@@ -150,6 +167,24 @@ impl RBSLocation {
150167
}
151168
}
152169

170+
pub struct RBSLocationList {
171+
pointer: *mut rbs_location_list,
172+
}
173+
174+
impl RBSLocationList {
175+
pub fn new(pointer: *mut rbs_location_list) -> Self {
176+
Self { pointer }
177+
}
178+
179+
/// Returns an iterator over the locations.
180+
#[must_use]
181+
pub fn iter(&self) -> RBSLocationListIter {
182+
RBSLocationListIter {
183+
current: unsafe { (*self.pointer).head },
184+
}
185+
}
186+
}
187+
153188
pub struct RBSLocationListIter {
154189
current: *mut rbs_location_list_node_t,
155190
}
@@ -169,24 +204,6 @@ impl Iterator for RBSLocationListIter {
169204
}
170205
}
171206

172-
pub struct RBSLocationList {
173-
pointer: *mut rbs_location_list,
174-
}
175-
176-
impl RBSLocationList {
177-
pub fn new(pointer: *mut rbs_location_list) -> Self {
178-
Self { pointer }
179-
}
180-
181-
/// Returns an iterator over the locations.
182-
#[must_use]
183-
pub fn iter(&self) -> RBSLocationListIter {
184-
RBSLocationListIter {
185-
current: unsafe { (*self.pointer).head },
186-
}
187-
}
188-
}
189-
190207
#[derive(Debug)]
191208
pub struct RBSString {
192209
pointer: *const rbs_string_t,
@@ -222,23 +239,6 @@ impl SymbolNode {
222239
}
223240
}
224241

225-
impl KeywordNode {
226-
pub fn name(&self) -> &[u8] {
227-
unsafe {
228-
let constant_ptr = rbs_constant_pool_id_to_constant(
229-
&(*self.parser).constant_pool,
230-
(*self.pointer).constant_id,
231-
);
232-
if constant_ptr.is_null() {
233-
panic!("Constant ID for keyword is not present in the pool");
234-
}
235-
236-
let constant = &*constant_ptr;
237-
std::slice::from_raw_parts(constant.start, constant.length)
238-
}
239-
}
240-
}
241-
242242
#[cfg(test)]
243243
mod tests {
244244
use super::*;

0 commit comments

Comments
 (0)