1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Implementation of Ruby macros.
//!
//! Since macros are rely on the C preprocessor, or defined as `inline` C
//! functions, they are not available when linking libruby. In order to use the
//! libruby macros from Rust, `rb-sys` implements them using the following
//! strategies:
//!
//! 1. For stable versions of Ruby, the macros are implemented as Rust functions
//! 2. For ruby-head, the macros are implemented as C functions that are linked
//! into the crate.
use craterb_data_type_t;
use crateruby_value_type;
use crateget_default as api;
use crateStableApiDefinition;
use crateVALUE;
use c_void;
use ;
/// Emulates Ruby's "if" statement.
///
/// - @param[in] obj An arbitrary ruby object.
/// - @retval false `obj` is either ::RUBY_Qfalse or ::RUBY_Qnil.
/// - @retval true Anything else.
///
/// ```
/// use rb_sys::special_consts::*;
///
/// assert!(!TEST(Qfalse));
/// assert!(!TEST(Qnil));
/// assert!(TEST(Qtrue));
/// ```
/// Checks if the given object is nil.
///
/// - @param[in] obj An arbitrary ruby object.
/// - @retval true `obj` is ::RUBY_Qnil.
/// - @retval false Anything else.
///
/// ### Example
///
/// ```
/// use rb_sys::special_consts::*;
///
/// assert!(NIL_P(Qnil));
/// assert!(!NIL_P(Qtrue));
/// ```
/// Checks if the given object is a so-called Fixnum.
///
/// - @param[in] obj An arbitrary ruby object.
/// - @retval true `obj` is a Fixnum.
/// - @retval false Anything else.
/// - @note Fixnum was a thing in the 20th century, but it is rather an implementation detail today.
/// Checks if the given object is a static symbol.
///
/// - @param[in] obj An arbitrary ruby object.
/// - @retval true `obj` is a static symbol
/// - @retval false Anything else.
/// - @see RB_DYNAMIC_SYM_P()
/// - @see RB_SYMBOL_P()
/// - @note These days there are static and dynamic symbols, just like we once had Fixnum/Bignum back in the old days.
/// Get the backend storage of a Ruby array.
///
/// ### Safety
///
/// This function is unsafe because it dereferences a raw pointer and returns
/// raw pointers to Ruby memory. The caller must ensure that the pointer stays live
/// for the duration of usage the the underlying array (by either GC marking or
/// keeping the RArray on the stack).
///
/// - @param[in] a An object of ::RArray.
/// - @return Its backend storage.
pub unsafe
/// Get the length of a Ruby array.
///
/// ### Safety
///
/// This function is unsafe because it dereferences a raw pointer in order to
/// access internal Ruby memory.
///
/// - @param[in] a An object of ::RArray.
/// - @return Its length.
pub unsafe
/// Get the length of a Ruby string.
///
/// ### Safety
///
/// This function is unsafe because it dereferences a raw pointer in order to
/// access internal Ruby memory.
///
/// - @param[in] a An object of ::RString.
/// - @return Its length.
pub unsafe
/// Get the backend storage of a Ruby string.
///
/// ### Safety
///
/// This function is unsafe because it dereferences a raw pointer and returns
/// raw pointers to Ruby memory. The caller must ensure that the pointer stays live
/// for the duration of usage the the underlying array (by either GC marking or
/// keeping the RArray on the stack).
///
/// - @param[in] a An object of ::RString.
/// - @return Its backend storage
pub unsafe
/// Checks if the given object is a so-called Flonum.
///
/// @param[in] obj An arbitrary ruby object.
/// @retval true `obj` is a Flonum.
/// @retval false Anything else.
/// @see RB_FLOAT_TYPE_P()
/// @note These days there are Flonums and non-Flonum floats, just like we
/// once had Fixnum/Bignum back in the old days.
/// Checks if the given object is an immediate i.e. an object which has no
/// corresponding storage inside of the object space.
///
/// @param[in] obj An arbitrary ruby object.
/// @retval true `obj` is a Flonum.
/// @retval false Anything else.
/// @see RB_FLOAT_TYPE_P()
/// @note The concept of "immediate" is purely C specific.
/// Checks if the given object is of enum ::ruby_special_consts.
///
/// @param[in] obj An arbitrary ruby object.
/// @retval true `obj` is a special constant.
/// @retval false Anything else.
///
/// ### Example
///
/// ```
/// use rb_sys::special_consts::*;
///
/// assert!(SPECIAL_CONST_P(Qnil));
/// assert!(SPECIAL_CONST_P(Qtrue));
/// assert!(SPECIAL_CONST_P(Qfalse));
/// ```
/// Queries the type of the object.
///
/// @param[in] obj Object in question.
/// @pre `obj` must not be a special constant.
/// @return The type of `obj`.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
pub unsafe
/// Queries if the object is an instance of ::rb_cInteger.
///
/// @param[in] obj Object in question.
/// @retval true It is.
/// @retval false It isn't.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
pub unsafe
/// Queries if the object is a dynamic symbol.
///
/// @param[in] obj Object in question.
/// @retval true It is.
/// @retval false It isn't.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
pub unsafe
/// Queries if the object is an instance of ::rb_cSymbol.
///
/// @param[in] obj Object in question.
/// @retval true It is.
/// @retval false It isn't.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
pub unsafe
/// Identical to RB_BUILTIN_TYPE(), except it can also accept special constants.
///
/// @param[in] obj Object in question.
/// @return The type of `obj`.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
pub unsafe
/// Queries if the given object is of given type.
///
/// @param[in] obj An object.
/// @param[in] t A type.
/// @retval true `obj` is of type `t`.
/// @retval false Otherwise.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
// truffleruby provides its own implementation
pub unsafe
/// Queries if the object is an instance of ::rb_cFloat.
///
/// @param[in] obj Object in question.
/// @retval true It is.
/// @retval false It isn't.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// attemping to access the underlying [`RBasic`] struct.
pub unsafe
/// Checks if the given object is an RTypedData.
///
/// @param[in] obj Object in question.
/// @retval true It is an RTypedData.
/// @retval false It isn't an RTypedData.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// accessing the underlying data structure.
pub unsafe
/// Gets the data type information from an RTypedData object.
///
/// @param[in] obj An RTypedData object.
/// @return Pointer to the rb_data_type_t structure for this object.
///
/// # Safety
/// This function is unsafe because it dereferences a raw pointer to get
/// access to the underlying data type. The caller must ensure the object
/// is a valid RTypedData.
pub unsafe
/// Gets the data pointer from an RTypedData object.
///
/// @param[in] obj An RTypedData object.
/// @return Pointer to the wrapped C struct.
///
/// # Safety
/// This function is unsafe because it dereferences a raw pointer to get
/// access to the underlying data. The caller must ensure the object
/// is a valid RTypedData.
pub unsafe
/// Checks if the bignum is positive.
///
/// @param[in] b An object of RBignum.
/// @retval false `b` is less than zero.
/// @retval true Otherwise.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// accessing the underlying bignum structure.
pub unsafe
/// Checks if the bignum is negative.
///
/// @param[in] b An object of RBignum.
/// @retval true `b` is less than zero.
/// @retval false Otherwise.
///
/// # Safety
/// This function is unsafe because it could dereference a raw pointer when
/// accessing the underlying bignum structure.
pub unsafe
/// Convert ID to Symbol (akin to `ID2SYM` or `RB_ID2SYM`).
///
/// Converts an internal ID to its corresponding Symbol VALUE.
/// This is a safe operation - just bit manipulation for static symbols.
///
/// @param[in] id An ID value.
/// @return The Symbol VALUE corresponding to the ID.
/// Alias for ID2SYM for compatibility with Ruby naming conventions.
/// Convert Symbol to ID (akin to `SYM2ID` or `RB_SYM2ID`).
///
/// Converts a Symbol VALUE to its internal ID representation.
///
/// @param[in] obj A Symbol VALUE.
/// @return The ID corresponding to the Symbol.
///
/// # Safety
/// - `obj` must be a valid Symbol VALUE
/// - For dynamic symbols, this may access the heap
pub unsafe
/// Alias for SYM2ID for compatibility with Ruby naming conventions.
///
/// # Safety
/// - `obj` must be a valid Symbol VALUE
/// - For dynamic symbols, this may access the heap
pub unsafe
/// Convert Fixnum to long (akin to `FIX2LONG`).
///
/// Extracts the integer value from a Fixnum VALUE by performing an arithmetic right shift.
///
/// # Safety
/// - `obj` must be a valid Fixnum VALUE (checked with FIXNUM_P)
/// - Behavior is undefined if called on non-Fixnum values
pub unsafe
/// Convert Fixnum to unsigned long (akin to `FIX2ULONG`).
///
/// Extracts the unsigned integer value from a Fixnum VALUE.
///
/// # Safety
/// - `obj` must be a valid positive Fixnum VALUE
/// - Behavior is undefined for negative fixnums
pub unsafe
/// Convert long to Fixnum (akin to `LONG2FIX`).
///
/// Creates a Fixnum VALUE from a long integer.
///
/// # Safety
/// - `val` must be within the valid Fixnum range (use FIXABLE to check)
/// - Behavior is undefined if value is out of range
pub unsafe
/// Check if long value can be represented as Fixnum (akin to `FIXABLE`).
///
/// Returns true if the value fits within the Fixnum range.
/// Check if unsigned long value can be represented as positive Fixnum (akin to `POSFIXABLE`).
///
/// Returns true if the unsigned value fits within the positive Fixnum range.
/// Convert Ruby Integer to long (akin to `NUM2LONG`).
///
/// Converts any Ruby Integer (Fixnum or Bignum) to a C long.
/// May raise a RangeError exception if the value is out of range.
///
/// # Safety
/// - `obj` must be a valid Integer VALUE
/// - May call into Ruby runtime and potentially raise exceptions
/// - May trigger garbage collection
pub unsafe
/// Convert Ruby Integer to unsigned long (akin to `NUM2ULONG`).
///
/// Converts any Ruby Integer (Fixnum or Bignum) to a C unsigned long.
/// May raise a RangeError exception if the value is out of range or negative.
///
/// # Safety
/// - `obj` must be a valid Integer VALUE
/// - May call into Ruby runtime and potentially raise exceptions
/// - May trigger garbage collection
pub unsafe
/// Convert long to Ruby Integer (akin to `LONG2NUM`).
///
/// Creates a Ruby Integer (Fixnum or Bignum) from a C long.
/// Uses Fixnum if possible, otherwise allocates a Bignum.
/// Convert unsigned long to Ruby Integer (akin to `ULONG2NUM`).
///
/// Creates a Ruby Integer (Fixnum or Bignum) from a C unsigned long.
/// Uses Fixnum if possible, otherwise allocates a Bignum.