rubydex 0.1.0.beta11 → 0.1.0.beta13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +23 -23
  3. data/README.md +125 -125
  4. data/THIRD_PARTY_LICENSES.html +2018 -945
  5. data/exe/rdx +47 -47
  6. data/ext/rubydex/declaration.c +453 -388
  7. data/ext/rubydex/declaration.h +23 -23
  8. data/ext/rubydex/definition.c +284 -197
  9. data/ext/rubydex/definition.h +28 -28
  10. data/ext/rubydex/diagnostic.c +6 -6
  11. data/ext/rubydex/diagnostic.h +11 -11
  12. data/ext/rubydex/document.c +97 -98
  13. data/ext/rubydex/document.h +10 -10
  14. data/ext/rubydex/extconf.rb +146 -127
  15. data/ext/rubydex/graph.c +701 -512
  16. data/ext/rubydex/graph.h +10 -10
  17. data/ext/rubydex/handle.h +44 -44
  18. data/ext/rubydex/location.c +22 -22
  19. data/ext/rubydex/location.h +15 -15
  20. data/ext/rubydex/reference.c +123 -104
  21. data/ext/rubydex/reference.h +15 -16
  22. data/ext/rubydex/rubydex.c +22 -22
  23. data/ext/rubydex/utils.c +108 -86
  24. data/ext/rubydex/utils.h +34 -28
  25. data/lib/rubydex/comment.rb +17 -17
  26. data/lib/rubydex/declaration.rb +11 -0
  27. data/lib/rubydex/diagnostic.rb +21 -21
  28. data/lib/rubydex/failures.rb +15 -15
  29. data/lib/rubydex/graph.rb +98 -92
  30. data/lib/rubydex/keyword.rb +17 -0
  31. data/lib/rubydex/keyword_parameter.rb +13 -0
  32. data/lib/rubydex/location.rb +90 -90
  33. data/lib/rubydex/mixin.rb +22 -0
  34. data/lib/rubydex/version.rb +5 -5
  35. data/lib/rubydex.rb +24 -20
  36. data/rbi/rubydex.rbi +425 -310
  37. data/rust/Cargo.lock +1851 -1851
  38. data/rust/Cargo.toml +29 -29
  39. data/rust/about.toml +10 -10
  40. data/rust/{about.hbs → about_templates/about.hbs} +81 -78
  41. data/rust/about_templates/mingw_licenses.hbs +1071 -0
  42. data/rust/rubydex/Cargo.toml +42 -42
  43. data/rust/rubydex/src/compile_assertions.rs +13 -13
  44. data/rust/rubydex/src/diagnostic.rs +110 -109
  45. data/rust/rubydex/src/errors.rs +28 -28
  46. data/rust/rubydex/src/indexing/local_graph.rs +224 -224
  47. data/rust/rubydex/src/indexing/rbs_indexer.rs +1551 -1554
  48. data/rust/rubydex/src/indexing/ruby_indexer.rs +2329 -6753
  49. data/rust/rubydex/src/indexing/ruby_indexer_tests.rs +4962 -0
  50. data/rust/rubydex/src/indexing.rs +210 -210
  51. data/rust/rubydex/src/integrity.rs +279 -278
  52. data/rust/rubydex/src/job_queue.rs +199 -205
  53. data/rust/rubydex/src/lib.rs +17 -17
  54. data/rust/rubydex/src/listing.rs +371 -272
  55. data/rust/rubydex/src/main.rs +160 -160
  56. data/rust/rubydex/src/model/built_in.rs +83 -0
  57. data/rust/rubydex/src/model/comment.rs +24 -24
  58. data/rust/rubydex/src/model/declaration.rs +679 -588
  59. data/rust/rubydex/src/model/definitions.rs +1682 -1602
  60. data/rust/rubydex/src/model/document.rs +222 -252
  61. data/rust/rubydex/src/model/encoding.rs +22 -22
  62. data/rust/rubydex/src/model/graph.rs +3782 -3556
  63. data/rust/rubydex/src/model/id.rs +110 -110
  64. data/rust/rubydex/src/model/identity_maps.rs +58 -58
  65. data/rust/rubydex/src/model/ids.rs +60 -38
  66. data/rust/rubydex/src/model/keywords.rs +256 -256
  67. data/rust/rubydex/src/model/name.rs +298 -298
  68. data/rust/rubydex/src/model/references.rs +111 -111
  69. data/rust/rubydex/src/model/string_ref.rs +50 -50
  70. data/rust/rubydex/src/model/visibility.rs +41 -41
  71. data/rust/rubydex/src/model.rs +15 -14
  72. data/rust/rubydex/src/offset.rs +147 -147
  73. data/rust/rubydex/src/position.rs +6 -6
  74. data/rust/rubydex/src/query.rs +1841 -1700
  75. data/rust/rubydex/src/resolution.rs +1852 -5895
  76. data/rust/rubydex/src/resolution_tests.rs +4701 -0
  77. data/rust/rubydex/src/stats/memory.rs +71 -71
  78. data/rust/rubydex/src/stats/orphan_report.rs +264 -263
  79. data/rust/rubydex/src/stats/timer.rs +127 -127
  80. data/rust/rubydex/src/stats.rs +11 -11
  81. data/rust/rubydex/src/test_utils/context.rs +226 -226
  82. data/rust/rubydex/src/test_utils/graph_test.rs +730 -679
  83. data/rust/rubydex/src/test_utils/local_graph_test.rs +602 -602
  84. data/rust/rubydex/src/test_utils.rs +52 -52
  85. data/rust/rubydex/src/visualization/dot.rs +192 -176
  86. data/rust/rubydex/src/visualization.rs +6 -6
  87. data/rust/rubydex/tests/cli.rs +185 -167
  88. data/rust/rubydex-mcp/Cargo.toml +28 -28
  89. data/rust/rubydex-mcp/src/main.rs +48 -48
  90. data/rust/rubydex-mcp/src/server.rs +1145 -1145
  91. data/rust/rubydex-mcp/src/tools.rs +49 -49
  92. data/rust/rubydex-mcp/tests/mcp.rs +302 -302
  93. data/rust/rubydex-sys/Cargo.toml +20 -20
  94. data/rust/rubydex-sys/build.rs +14 -14
  95. data/rust/rubydex-sys/cbindgen.toml +12 -12
  96. data/rust/rubydex-sys/src/declaration_api.rs +485 -469
  97. data/rust/rubydex-sys/src/definition_api.rs +443 -352
  98. data/rust/rubydex-sys/src/diagnostic_api.rs +99 -99
  99. data/rust/rubydex-sys/src/document_api.rs +85 -54
  100. data/rust/rubydex-sys/src/graph_api.rs +1017 -700
  101. data/rust/rubydex-sys/src/lib.rs +79 -9
  102. data/rust/rubydex-sys/src/location_api.rs +79 -79
  103. data/rust/rubydex-sys/src/name_api.rs +187 -135
  104. data/rust/rubydex-sys/src/reference_api.rs +267 -195
  105. data/rust/rubydex-sys/src/utils.rs +70 -70
  106. data/rust/rustfmt.toml +2 -2
  107. metadata +16 -9
  108. data/lib/rubydex/librubydex_sys.so +0 -0
@@ -1,195 +1,267 @@
1
- //! C API for exposing references through ID handles (like definitions)
2
-
3
- use std::ffi::CString;
4
-
5
- use crate::graph_api::{GraphPointer, with_graph};
6
- use crate::location_api::{Location, create_location_for_uri_and_offset};
7
- use libc::c_char;
8
- use rubydex::model::ids::ReferenceId;
9
-
10
- /// Kind of reference for FFI dispatch
11
- #[repr(C)]
12
- #[derive(Copy, Clone, Debug, PartialEq, Eq)]
13
- pub enum ReferenceKind {
14
- Constant = 0,
15
- Method = 1,
16
- }
17
-
18
- #[repr(C)]
19
- #[derive(Debug, Clone, Copy)]
20
- pub struct CReference {
21
- id: u64,
22
- kind: ReferenceKind,
23
- }
24
-
25
- impl CReference {
26
- #[must_use]
27
- pub fn new(id: u64, kind: ReferenceKind) -> Self {
28
- Self { id, kind }
29
- }
30
- }
31
-
32
- #[derive(Debug)]
33
- pub struct ReferencesIter {
34
- pub entries: Box<[CReference]>,
35
- pub index: usize,
36
- }
37
-
38
- impl ReferencesIter {
39
- #[must_use]
40
- pub fn new(entries: Box<[CReference]>) -> *mut ReferencesIter {
41
- Box::into_raw(Box::new(ReferencesIter { entries, index: 0 }))
42
- }
43
- }
44
-
45
- /// Returns the total number of entries in the iterator snapshot.
46
- ///
47
- /// # Safety
48
- /// - `iter` must be a valid pointer previously returned by `ReferencesIter::new`.
49
- #[unsafe(no_mangle)]
50
- pub unsafe extern "C" fn rdx_references_iter_len(iter: *const ReferencesIter) -> usize {
51
- if iter.is_null() {
52
- return 0;
53
- }
54
- unsafe { (&*iter).entries.len() }
55
- }
56
-
57
- /// Advances the iterator and writes the next entry into `out_ref`.
58
- /// Returns `true` if an entry was written, or `false` if the iterator is exhausted or inputs are invalid.
59
- ///
60
- /// # Safety
61
- /// - `iter` must be a valid pointer previously returned by `ReferencesIter::new`.
62
- /// - `out_ref` must be a valid, writable pointer.
63
- #[unsafe(no_mangle)]
64
- pub unsafe extern "C" fn rdx_references_iter_next(iter: *mut ReferencesIter, out_ref: *mut CReference) -> bool {
65
- if iter.is_null() || out_ref.is_null() {
66
- return false;
67
- }
68
-
69
- let it = unsafe { &mut *iter };
70
- if it.index >= it.entries.len() {
71
- return false;
72
- }
73
-
74
- let entry = it.entries[it.index];
75
- it.index += 1;
76
- unsafe {
77
- *out_ref = entry;
78
- }
79
- true
80
- }
81
-
82
- /// Frees an iterator created by `ReferencesIter::new`.
83
- ///
84
- /// # Safety
85
- /// - `iter` must be a pointer previously returned by `ReferencesIter::new`.
86
- /// - `iter` must not be used after being freed.
87
- #[unsafe(no_mangle)]
88
- pub unsafe extern "C" fn rdx_references_iter_free(iter: *mut ReferencesIter) {
89
- if iter.is_null() {
90
- return;
91
- }
92
- unsafe {
93
- let _ = Box::from_raw(iter);
94
- }
95
- }
96
-
97
- /// Returns the UTF-8 name string for a constant reference id.
98
- /// Caller must free with `free_c_string`.
99
- ///
100
- /// # Safety
101
- ///
102
- /// Assumes pointer is valid.
103
- ///
104
- /// # Panics
105
- ///
106
- /// This function will panic if the reference cannot be found.
107
- #[unsafe(no_mangle)]
108
- pub unsafe extern "C" fn rdx_constant_reference_name(pointer: GraphPointer, reference_id: u64) -> *const c_char {
109
- with_graph(pointer, |graph| {
110
- let ref_id = ReferenceId::new(reference_id);
111
- let reference = graph.constant_references().get(&ref_id).expect("Reference not found");
112
- let name = graph.names().get(reference.name_id()).expect("Name ID should exist");
113
-
114
- let name_string = graph
115
- .strings()
116
- .get(name.str())
117
- .expect("String ID should exist")
118
- .to_string();
119
- CString::new(name_string).unwrap().into_raw().cast_const()
120
- })
121
- }
122
-
123
- /// Returns the UTF-8 name string for a method reference id.
124
- /// Caller must free with `free_c_string`.
125
- ///
126
- /// # Safety
127
- ///
128
- /// Assumes pointer is valid.
129
- ///
130
- /// # Panics
131
- ///
132
- /// This function will panic if the reference cannot be found.
133
- #[unsafe(no_mangle)]
134
- pub unsafe extern "C" fn rdx_method_reference_name(pointer: GraphPointer, reference_id: u64) -> *const c_char {
135
- with_graph(pointer, |graph| {
136
- let ref_id = ReferenceId::new(reference_id);
137
- let reference = graph.method_references().get(&ref_id).expect("Reference not found");
138
- let name = graph
139
- .strings()
140
- .get(reference.str())
141
- .expect("Name ID should exist")
142
- .to_string();
143
- CString::new(name).unwrap().into_raw().cast_const()
144
- })
145
- }
146
-
147
- /// Returns a newly allocated `Location` for the given constant reference id.
148
- /// Caller must free the returned pointer with `rdx_location_free`.
149
- ///
150
- /// # Safety
151
- ///
152
- /// - `pointer` must be a valid pointer previously returned by `rdx_graph_new`.
153
- /// - `reference_id` must be a valid reference id.
154
- ///
155
- /// # Panics
156
- ///
157
- /// This function will panic if a reference or document cannot be found.
158
- #[unsafe(no_mangle)]
159
- pub unsafe extern "C" fn rdx_constant_reference_location(pointer: GraphPointer, reference_id: u64) -> *mut Location {
160
- with_graph(pointer, |graph| {
161
- let ref_id = ReferenceId::new(reference_id);
162
- let reference = graph.constant_references().get(&ref_id).expect("Reference not found");
163
- let document = graph
164
- .documents()
165
- .get(&reference.uri_id())
166
- .expect("Document should exist");
167
-
168
- create_location_for_uri_and_offset(graph, document, reference.offset())
169
- })
170
- }
171
-
172
- /// Returns a newly allocated `Location` for the given method reference id.
173
- /// Caller must free the returned pointer with `rdx_location_free`.
174
- ///
175
- /// # Safety
176
- ///
177
- /// - `pointer` must be a valid pointer previously returned by `rdx_graph_new`.
178
- /// - `reference_id` must be a valid reference id.
179
- ///
180
- /// # Panics
181
- ///
182
- /// This function will panic if a reference or document cannot be found.
183
- #[unsafe(no_mangle)]
184
- pub unsafe extern "C" fn rdx_method_reference_location(pointer: GraphPointer, reference_id: u64) -> *mut Location {
185
- with_graph(pointer, |graph| {
186
- let ref_id = ReferenceId::new(reference_id);
187
- let reference = graph.method_references().get(&ref_id).expect("Reference not found");
188
- let document = graph
189
- .documents()
190
- .get(&reference.uri_id())
191
- .expect("Document should exist");
192
-
193
- create_location_for_uri_and_offset(graph, document, reference.offset())
194
- })
195
- }
1
+ //! C API for exposing references through ID handles (like definitions)
2
+
3
+ use std::ffi::CString;
4
+ use std::ptr;
5
+
6
+ use crate::declaration_api::CDeclaration;
7
+ use crate::graph_api::{GraphPointer, with_graph};
8
+ use crate::location_api::{Location, create_location_for_uri_and_offset};
9
+ use libc::c_char;
10
+ use rubydex::model::graph::Graph;
11
+ use rubydex::model::ids::{ConstantReferenceId, MethodReferenceId};
12
+ use rubydex::model::name::NameRef;
13
+
14
+ #[repr(C)]
15
+ #[derive(Debug, Clone, Copy)]
16
+ pub struct CConstantReference {
17
+ pub id: u64,
18
+ pub declaration_id: u64,
19
+ }
20
+
21
+ impl CConstantReference {
22
+ /// Build a `CConstantReference` from a graph and reference ID. Sets `declaration_id` to 0 when the reference is
23
+ /// unresolved.
24
+ ///
25
+ /// # Panics
26
+ ///
27
+ /// This function will panic if there's inconsistent data in the graph
28
+ #[must_use]
29
+ pub fn from_id(graph: &Graph, ref_id: ConstantReferenceId) -> Self {
30
+ let reference = graph
31
+ .constant_references()
32
+ .get(&ref_id)
33
+ .expect("Constant reference not found");
34
+
35
+ let name_ref = graph.names().get(reference.name_id()).expect("Name ID should exist");
36
+
37
+ let declaration_id = match name_ref {
38
+ NameRef::Resolved(resolved) => **resolved.declaration_id(),
39
+ NameRef::Unresolved(_) => 0,
40
+ };
41
+
42
+ Self {
43
+ id: *ref_id,
44
+ declaration_id,
45
+ }
46
+ }
47
+ }
48
+
49
+ #[derive(Debug)]
50
+ pub struct ConstantReferencesIter {
51
+ entries: Box<[CConstantReference]>,
52
+ index: usize,
53
+ }
54
+
55
+ iterator!(ConstantReferencesIter, entries: CConstantReference);
56
+
57
+ /// # Safety
58
+ /// `iter` must be a valid pointer previously returned by `ConstantReferencesIter::new`.
59
+ #[unsafe(no_mangle)]
60
+ pub unsafe extern "C" fn rdx_constant_references_iter_len(iter: *const ConstantReferencesIter) -> usize {
61
+ unsafe { ConstantReferencesIter::len(iter) }
62
+ }
63
+
64
+ /// # Safety
65
+ /// - `iter` must be a valid pointer previously returned by `ConstantReferencesIter::new`.
66
+ /// - `out` must be a valid, writable pointer.
67
+ #[unsafe(no_mangle)]
68
+ pub unsafe extern "C" fn rdx_constant_references_iter_next(
69
+ iter: *mut ConstantReferencesIter,
70
+ out: *mut CConstantReference,
71
+ ) -> bool {
72
+ unsafe { ConstantReferencesIter::next(iter, out) }
73
+ }
74
+
75
+ /// # Safety
76
+ /// - `iter` must be a pointer previously returned by `ConstantReferencesIter::new`.
77
+ /// - `iter` must not be used after being freed.
78
+ #[unsafe(no_mangle)]
79
+ pub unsafe extern "C" fn rdx_constant_references_iter_free(iter: *mut ConstantReferencesIter) {
80
+ unsafe { ConstantReferencesIter::free(iter) }
81
+ }
82
+
83
+ #[repr(C)]
84
+ #[derive(Debug, Clone, Copy)]
85
+ pub struct CMethodReference {
86
+ pub id: u64,
87
+ }
88
+
89
+ #[derive(Debug)]
90
+ pub struct MethodReferencesIter {
91
+ entries: Box<[CMethodReference]>,
92
+ index: usize,
93
+ }
94
+
95
+ iterator!(MethodReferencesIter, entries: CMethodReference);
96
+
97
+ /// # Safety
98
+ /// `iter` must be a valid pointer previously returned by `MethodReferencesIter::new`.
99
+ #[unsafe(no_mangle)]
100
+ pub unsafe extern "C" fn rdx_method_references_iter_len(iter: *const MethodReferencesIter) -> usize {
101
+ unsafe { MethodReferencesIter::len(iter) }
102
+ }
103
+
104
+ /// # Safety
105
+ /// - `iter` must be a valid pointer previously returned by `MethodReferencesIter::new`.
106
+ /// - `out` must be a valid, writable pointer.
107
+ #[unsafe(no_mangle)]
108
+ pub unsafe extern "C" fn rdx_method_references_iter_next(
109
+ iter: *mut MethodReferencesIter,
110
+ out: *mut CMethodReference,
111
+ ) -> bool {
112
+ unsafe { MethodReferencesIter::next(iter, out) }
113
+ }
114
+
115
+ /// # Safety
116
+ /// - `iter` must be a pointer previously returned by `MethodReferencesIter::new`.
117
+ /// - `iter` must not be used after being freed.
118
+ #[unsafe(no_mangle)]
119
+ pub unsafe extern "C" fn rdx_method_references_iter_free(iter: *mut MethodReferencesIter) {
120
+ unsafe { MethodReferencesIter::free(iter) }
121
+ }
122
+
123
+ /// Returns the UTF-8 name string for a constant reference id.
124
+ /// Caller must free with `free_c_string`.
125
+ ///
126
+ /// # Safety
127
+ ///
128
+ /// Assumes pointer is valid.
129
+ ///
130
+ /// # Panics
131
+ ///
132
+ /// This function will panic if the reference cannot be found.
133
+ #[unsafe(no_mangle)]
134
+ pub unsafe extern "C" fn rdx_constant_reference_name(pointer: GraphPointer, reference_id: u64) -> *const c_char {
135
+ with_graph(pointer, |graph| {
136
+ let ref_id = ConstantReferenceId::new(reference_id);
137
+ let reference = graph.constant_references().get(&ref_id).expect("Reference not found");
138
+ let name = graph.names().get(reference.name_id()).expect("Name ID should exist");
139
+
140
+ let name_string = graph
141
+ .strings()
142
+ .get(name.str())
143
+ .expect("String ID should exist")
144
+ .to_string();
145
+ CString::new(name_string).unwrap().into_raw().cast_const()
146
+ })
147
+ }
148
+
149
+ /// Returns the UTF-8 name string for a method reference id.
150
+ /// Caller must free with `free_c_string`.
151
+ ///
152
+ /// # Safety
153
+ ///
154
+ /// Assumes pointer is valid.
155
+ ///
156
+ /// # Panics
157
+ ///
158
+ /// This function will panic if the reference cannot be found.
159
+ #[unsafe(no_mangle)]
160
+ pub unsafe extern "C" fn rdx_method_reference_name(pointer: GraphPointer, reference_id: u64) -> *const c_char {
161
+ with_graph(pointer, |graph| {
162
+ let ref_id = MethodReferenceId::new(reference_id);
163
+ let reference = graph.method_references().get(&ref_id).expect("Reference not found");
164
+ let name = graph
165
+ .strings()
166
+ .get(reference.str())
167
+ .expect("Name ID should exist")
168
+ .to_string();
169
+ CString::new(name).unwrap().into_raw().cast_const()
170
+ })
171
+ }
172
+
173
+ /// Returns a newly allocated `Location` for the given constant reference id.
174
+ /// Caller must free the returned pointer with `rdx_location_free`.
175
+ ///
176
+ /// # Safety
177
+ ///
178
+ /// - `pointer` must be a valid pointer previously returned by `rdx_graph_new`.
179
+ /// - `reference_id` must be a valid reference id.
180
+ ///
181
+ /// # Panics
182
+ ///
183
+ /// This function will panic if a reference or document cannot be found.
184
+ #[unsafe(no_mangle)]
185
+ pub unsafe extern "C" fn rdx_constant_reference_location(pointer: GraphPointer, reference_id: u64) -> *mut Location {
186
+ with_graph(pointer, |graph| {
187
+ let ref_id = ConstantReferenceId::new(reference_id);
188
+ let reference = graph.constant_references().get(&ref_id).expect("Reference not found");
189
+ let document = graph
190
+ .documents()
191
+ .get(&reference.uri_id())
192
+ .expect("Document should exist");
193
+
194
+ create_location_for_uri_and_offset(graph, document, reference.offset())
195
+ })
196
+ }
197
+
198
+ /// Returns the declaration that the given resolved constant reference points to. Returns NULL if the reference is
199
+ /// unresolved. Caller must free with `free_c_declaration`.
200
+ ///
201
+ /// # Safety
202
+ ///
203
+ /// Assumes pointer is valid.
204
+ ///
205
+ /// # Panics
206
+ ///
207
+ /// This function will panic if the reference cannot be found.
208
+ #[unsafe(no_mangle)]
209
+ pub unsafe extern "C" fn rdx_resolved_constant_reference_declaration(
210
+ pointer: GraphPointer,
211
+ reference_id: u64,
212
+ ) -> *const CDeclaration {
213
+ with_graph(pointer, |graph| {
214
+ let ref_id = ConstantReferenceId::new(reference_id);
215
+ let reference = graph.constant_references().get(&ref_id).expect("Reference not found");
216
+ let name_ref = graph.names().get(reference.name_id()).expect("Name ID should exist");
217
+
218
+ match name_ref {
219
+ NameRef::Resolved(resolved) => {
220
+ let decl_id = *resolved.declaration_id();
221
+ let decl = graph.declarations().get(&decl_id).expect("Declaration not found");
222
+ Box::into_raw(Box::new(CDeclaration::from_declaration(decl_id, decl))).cast_const()
223
+ }
224
+ NameRef::Unresolved(_) => ptr::null(),
225
+ }
226
+ })
227
+ }
228
+
229
+ /// Returns a newly allocated `Location` for the given method reference id.
230
+ /// Caller must free the returned pointer with `rdx_location_free`.
231
+ ///
232
+ /// # Safety
233
+ ///
234
+ /// - `pointer` must be a valid pointer previously returned by `rdx_graph_new`.
235
+ /// - `reference_id` must be a valid reference id.
236
+ ///
237
+ /// # Panics
238
+ ///
239
+ /// This function will panic if a reference or document cannot be found.
240
+ #[unsafe(no_mangle)]
241
+ pub unsafe extern "C" fn rdx_method_reference_location(pointer: GraphPointer, reference_id: u64) -> *mut Location {
242
+ with_graph(pointer, |graph| {
243
+ let ref_id = MethodReferenceId::new(reference_id);
244
+ let reference = graph.method_references().get(&ref_id).expect("Reference not found");
245
+ let document = graph
246
+ .documents()
247
+ .get(&reference.uri_id())
248
+ .expect("Document should exist");
249
+
250
+ create_location_for_uri_and_offset(graph, document, reference.offset())
251
+ })
252
+ }
253
+
254
+ /// Frees a `CConstantReference` previously returned by an FFI function.
255
+ ///
256
+ /// # Safety
257
+ /// - `ptr` must be a valid pointer previously returned by an FFI function that allocates a `CConstantReference`, or
258
+ /// NULL.
259
+ /// - `ptr` must not be used after being freed.
260
+ #[unsafe(no_mangle)]
261
+ pub unsafe extern "C" fn free_c_constant_reference(ptr: *const CConstantReference) {
262
+ if !ptr.is_null() {
263
+ unsafe {
264
+ let _ = Box::from_raw(ptr.cast_mut());
265
+ }
266
+ }
267
+ }
@@ -1,70 +1,70 @@
1
- use libc::{c_char, size_t};
2
- use std::ffi::{CStr, CString};
3
- use std::slice;
4
- use std::str::Utf8Error;
5
-
6
- /// Converts a C array of strings into a Vec<String>
7
- ///
8
- /// # Safety
9
- ///
10
- /// This function is unsafe because it attempts to instantiate a Vec<String> from a raw char** pointer
11
- ///
12
- /// # Errors
13
- ///
14
- /// This function errors if any of the strings inside the array contain invalid UTF-8 data
15
- pub unsafe fn convert_double_pointer_to_vec(data: *const *const c_char, len: size_t) -> Result<Vec<String>, Utf8Error> {
16
- unsafe {
17
- slice::from_raw_parts(data, len)
18
- .iter()
19
- .map(|arg| CStr::from_ptr(*arg).to_str().map(ToString::to_string))
20
- .collect()
21
- }
22
- }
23
-
24
- /// # Safety
25
- ///
26
- /// This function is unsafe because it dereferences the char pointer, which needs to be valid for the duration of the
27
- /// function
28
- ///
29
- /// # Errors
30
- ///
31
- /// This function errors if any of the strings inside the array contain invalid UTF-8 data
32
- pub unsafe fn convert_char_ptr_to_string(data: *const c_char) -> Result<String, Utf8Error> {
33
- unsafe { CStr::from_ptr(data).to_str().map(ToString::to_string) }
34
- }
35
-
36
- /// Frees a `CString` allocated on the Rust side
37
- #[unsafe(no_mangle)]
38
- pub extern "C" fn free_c_string(ptr: *const c_char) {
39
- unsafe {
40
- let _ = CString::from_raw(ptr.cast_mut());
41
- }
42
- }
43
-
44
- /// Frees a boxed u64 allocated on the Rust side
45
- #[unsafe(no_mangle)]
46
- pub extern "C" fn free_u64(ptr: *const u64) {
47
- unsafe {
48
- let _ = Box::from_raw(ptr.cast_mut());
49
- }
50
- }
51
-
52
- /// Frees an array of C strings allocated by Rust.
53
- ///
54
- /// # Safety
55
- /// - `ptr` must be a pointer to a boxed slice of C strings previously allocated by this crate.
56
- /// - `count` must be the length of the array.
57
- /// - `ptr` must not be used after being freed.
58
- #[unsafe(no_mangle)]
59
- pub unsafe extern "C" fn free_c_string_array(ptr: *const *const c_char, count: usize) {
60
- if ptr.is_null() {
61
- return;
62
- }
63
-
64
- let slice = unsafe { Box::from_raw(std::ptr::slice_from_raw_parts_mut(ptr.cast_mut(), count)) };
65
- let _: Vec<_> = slice
66
- .iter()
67
- .filter(|p| !p.is_null())
68
- .map(|arg| unsafe { CString::from_raw((*arg).cast_mut()) })
69
- .collect();
70
- }
1
+ use libc::{c_char, size_t};
2
+ use std::ffi::{CStr, CString};
3
+ use std::slice;
4
+ use std::str::Utf8Error;
5
+
6
+ /// Converts a C array of strings into a Vec<String>
7
+ ///
8
+ /// # Safety
9
+ ///
10
+ /// This function is unsafe because it attempts to instantiate a Vec<String> from a raw char** pointer
11
+ ///
12
+ /// # Errors
13
+ ///
14
+ /// This function errors if any of the strings inside the array contain invalid UTF-8 data
15
+ pub unsafe fn convert_double_pointer_to_vec(data: *const *const c_char, len: size_t) -> Result<Vec<String>, Utf8Error> {
16
+ unsafe {
17
+ slice::from_raw_parts(data, len)
18
+ .iter()
19
+ .map(|arg| CStr::from_ptr(*arg).to_str().map(ToString::to_string))
20
+ .collect()
21
+ }
22
+ }
23
+
24
+ /// # Safety
25
+ ///
26
+ /// This function is unsafe because it dereferences the char pointer, which needs to be valid for the duration of the
27
+ /// function
28
+ ///
29
+ /// # Errors
30
+ ///
31
+ /// This function errors if any of the strings inside the array contain invalid UTF-8 data
32
+ pub unsafe fn convert_char_ptr_to_string(data: *const c_char) -> Result<String, Utf8Error> {
33
+ unsafe { CStr::from_ptr(data).to_str().map(ToString::to_string) }
34
+ }
35
+
36
+ /// Frees a `CString` allocated on the Rust side
37
+ #[unsafe(no_mangle)]
38
+ pub extern "C" fn free_c_string(ptr: *const c_char) {
39
+ unsafe {
40
+ let _ = CString::from_raw(ptr.cast_mut());
41
+ }
42
+ }
43
+
44
+ /// Frees a boxed u64 allocated on the Rust side
45
+ #[unsafe(no_mangle)]
46
+ pub extern "C" fn free_u64(ptr: *const u64) {
47
+ unsafe {
48
+ let _ = Box::from_raw(ptr.cast_mut());
49
+ }
50
+ }
51
+
52
+ /// Frees an array of C strings allocated by Rust.
53
+ ///
54
+ /// # Safety
55
+ /// - `ptr` must be a pointer to a boxed slice of C strings previously allocated by this crate.
56
+ /// - `count` must be the length of the array.
57
+ /// - `ptr` must not be used after being freed.
58
+ #[unsafe(no_mangle)]
59
+ pub unsafe extern "C" fn free_c_string_array(ptr: *const *const c_char, count: usize) {
60
+ if ptr.is_null() {
61
+ return;
62
+ }
63
+
64
+ let slice = unsafe { Box::from_raw(std::ptr::slice_from_raw_parts_mut(ptr.cast_mut(), count)) };
65
+ let _: Vec<_> = slice
66
+ .iter()
67
+ .filter(|p| !p.is_null())
68
+ .map(|arg| unsafe { CString::from_raw((*arg).cast_mut()) })
69
+ .collect();
70
+ }
data/rust/rustfmt.toml CHANGED
@@ -1,2 +1,2 @@
1
- max_width = 120
2
- edition = "2024"
1
+ max_width = 120
2
+ edition = "2024"