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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Types and functions for working with Ruby's Struct class.

use std::{
    borrow::Cow,
    ffi::CString,
    fmt,
    ops::Deref,
    os::raw::c_char,
    ptr::{null, NonNull},
    slice,
};

use rb_sys::{
    rb_struct_aref, rb_struct_aset, rb_struct_define, rb_struct_getmember, rb_struct_members,
    rb_struct_size, ruby_value_type, VALUE,
};

use crate::{
    class::RClass,
    debug_assert_value,
    error::{protect, Error},
    exception,
    object::Object,
    r_array::RArray,
    symbol::Symbol,
    try_convert::TryConvert,
    value::{self, Id, NonZeroValue, Value},
};

// Ruby provides some inline functions to get a pointer to the struct's
// contents, but we have to reimplement those for Rust. The for that we need
// the definition of RStruct, but that isn't public, so we have to duplicate it
// here.
mod sys {
    #[cfg(ruby_lt_3_0)]
    use rb_sys::ruby_fl_type::RUBY_FL_USHIFT;
    #[cfg(ruby_gte_3_0)]
    use rb_sys::ruby_fl_ushift::RUBY_FL_USHIFT;
    use rb_sys::{ruby_fl_type, RBasic, VALUE};

    #[cfg(ruby_gte_2_7)]
    pub const EMBED_LEN_MAX: u32 = rb_sys::ruby_rvalue_flags::RVALUE_EMBED_LEN_MAX as u32;

    #[cfg(ruby_lt_2_7)]
    pub const EMBED_LEN_MAX: u32 = 3;

    pub const EMBED_LEN_MASK: u32 =
        ruby_fl_type::RUBY_FL_USER2 as u32 | ruby_fl_type::RUBY_FL_USER1 as u32;
    pub const EMBED_LEN_SHIFT: u32 = RUBY_FL_USHIFT as u32 + 1;

    #[repr(C)]
    #[derive(Copy, Clone)]
    pub struct RStruct {
        pub basic: RBasic,
        pub as_: As,
    }
    #[repr(C)]
    #[derive(Copy, Clone)]
    pub union As {
        pub heap: Heap,
        pub ary: [VALUE; EMBED_LEN_MAX as usize],
    }
    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    pub struct Heap {
        pub len: std::os::raw::c_long,
        pub ptr: *const VALUE,
    }
}

/// A Value pointer to a RStruct struct, Ruby’s internal representation of
/// 'Structs'.
///
/// All [`Value`] methods should be available on this type through [`Deref`],
/// but some may be missed by this documentation.
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct RStruct(NonZeroValue);

impl RStruct {
    /// Return `Some(RStruct)` if `val` is a `RStruct`, `None` otherwise.
    #[inline]
    pub fn from_value(val: Value) -> Option<Self> {
        unsafe {
            (val.rb_type() == ruby_value_type::RUBY_T_STRUCT)
                .then(|| Self(NonZeroValue::new_unchecked(val)))
        }
    }

    pub(crate) unsafe fn from_rb_value_unchecked(val: VALUE) -> Self {
        Self(NonZeroValue::new_unchecked(Value::new(val)))
    }

    fn as_internal(self) -> NonNull<sys::RStruct> {
        // safe as inner value is NonZero
        unsafe { NonNull::new_unchecked(self.0.get().as_rb_value() as *mut _) }
    }

    /// Return the members of the struct as a slice of [`Value`]s. The order
    /// will be the order the of the member names when the struct class was
    /// defined.
    ///
    /// # Safety
    ///
    /// Ruby may modify or free the memory backing the returned slice, the
    /// caller must ensure this does not happen.
    pub unsafe fn as_slice(&self) -> &[Value] {
        self.as_slice_unconstrained()
    }

    pub(crate) unsafe fn as_slice_unconstrained<'a>(self) -> &'a [Value] {
        debug_assert_value!(self);
        let r_basic = self.r_basic_unchecked();
        let flags = r_basic.as_ref().flags;
        if (flags & sys::EMBED_LEN_MASK as VALUE) != 0 {
            let len = (flags & sys::EMBED_LEN_MASK as VALUE) >> sys::EMBED_LEN_SHIFT as VALUE;
            slice::from_raw_parts(
                &self.as_internal().as_ref().as_.ary as *const VALUE as *const Value,
                len as usize,
            )
        } else {
            let h = self.as_internal().as_ref().as_.heap;
            slice::from_raw_parts(h.ptr as *const Value, h.len as usize)
        }
    }

    /// Return the value for the member at `index`, where members are ordered
    /// as per the member names when the struct class was defined.
    pub fn get<T>(self, index: usize) -> Result<T, Error>
    where
        T: TryConvert,
    {
        unsafe {
            let slice = self.as_slice();
            slice
                .get(index)
                .ok_or_else(|| {
                    Error::new(
                        exception::index_error(),
                        format!(
                            "offset {} too large for struct(size:{})",
                            index,
                            slice.len()
                        ),
                    )
                })
                .and_then(|v| v.try_convert())
        }
    }

    /// Return the value for the member at `index`.
    ///
    /// `index` may be an integer, string, or [`Symbol`].
    pub fn aref<T, U>(self, index: T) -> Result<U, Error>
    where
        T: Into<Value>,
        U: TryConvert,
    {
        let index = index.into();
        protect(|| unsafe { Value::new(rb_struct_aref(self.as_rb_value(), index.as_rb_value())) })
            .and_then(|v| v.try_convert())
    }

    /// Return the value for the member at `index`.
    ///
    /// `index` may be an integer, string, or [`Symbol`].
    pub fn aset<T, U>(self, index: T, val: U) -> Result<(), Error>
    where
        T: Into<Value>,
        U: Into<Value>,
    {
        let index = index.into();
        let val = val.into();
        unsafe {
            protect(|| {
                Value::new(rb_struct_aset(
                    self.as_rb_value(),
                    index.as_rb_value(),
                    val.as_rb_value(),
                ))
            })?;
        }
        Ok(())
    }

    /// Returns the count of members this struct has.
    pub fn size(self) -> usize {
        unsafe {
            Value::new(rb_struct_size(self.as_rb_value()))
                .try_convert()
                .unwrap()
        }
    }

    /// Returns the member names for this struct as [`Symbol`]s.
    pub fn members(self) -> Result<Vec<Cow<'static, str>>, Error> {
        unsafe {
            let array = RArray::from_rb_value_unchecked(rb_struct_members(self.as_rb_value()));
            array
                .as_slice()
                .iter()
                .map(|v| Symbol::from_value(*v).unwrap().name())
                .collect()
        }
    }

    /// Return the value for the member named `id`.
    pub fn getmember<T, U>(self, id: T) -> Result<U, Error>
    where
        T: Into<Id>,
        U: TryConvert,
    {
        let id = id.into();
        protect(|| unsafe { Value::new(rb_struct_getmember(self.as_rb_value(), id.as_rb_id())) })
            .and_then(|v| v.try_convert())
    }
}

impl Deref for RStruct {
    type Target = Value;

    fn deref(&self) -> &Self::Target {
        self.0.get_ref()
    }
}

impl fmt::Display for RStruct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", unsafe { self.to_s_infallible() })
    }
}

impl fmt::Debug for RStruct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.inspect())
    }
}

impl From<RStruct> for Value {
    fn from(val: RStruct) -> Self {
        *val
    }
}

impl Object for RStruct {}

unsafe impl value::private::ReprValue for RStruct {
    fn to_value(self) -> Value {
        *self
    }

    unsafe fn from_value_unchecked(val: Value) -> Self {
        Self(NonZeroValue::new_unchecked(val))
    }
}

impl TryConvert for RStruct {
    fn try_convert(val: Value) -> Result<Self, Error> {
        Self::from_value(val).ok_or_else(|| {
            Error::new(
                exception::type_error(),
                format!("no implicit conversion of {} into Struct", unsafe {
                    val.classname()
                },),
            )
        })
    }
}

/// Define a Ruby Struct class.
pub fn define_struct<T>(name: Option<&str>, members: T) -> Result<RClass, Error>
where
    T: StructMembers,
{
    members.define(name)
}

mod private {
    use super::*;

    pub trait StructMembers {
        fn define(self, name: Option<&str>) -> Result<RClass, Error>;
    }
}
use private::StructMembers;

impl StructMembers for (&str,) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str, &str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        let arg5 = CString::new(self.5).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                arg5.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str, &str, &str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        let arg5 = CString::new(self.5).unwrap();
        let arg6 = CString::new(self.6).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                arg5.as_ptr(),
                arg6.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str, &str, &str, &str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        let arg5 = CString::new(self.5).unwrap();
        let arg6 = CString::new(self.6).unwrap();
        let arg7 = CString::new(self.7).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                arg5.as_ptr(),
                arg6.as_ptr(),
                arg7.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str, &str, &str, &str, &str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        let arg5 = CString::new(self.5).unwrap();
        let arg6 = CString::new(self.6).unwrap();
        let arg7 = CString::new(self.7).unwrap();
        let arg8 = CString::new(self.8).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                arg5.as_ptr(),
                arg6.as_ptr(),
                arg7.as_ptr(),
                arg8.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers for (&str, &str, &str, &str, &str, &str, &str, &str, &str, &str) {
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        let arg5 = CString::new(self.5).unwrap();
        let arg6 = CString::new(self.6).unwrap();
        let arg7 = CString::new(self.7).unwrap();
        let arg8 = CString::new(self.8).unwrap();
        let arg9 = CString::new(self.9).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                arg5.as_ptr(),
                arg6.as_ptr(),
                arg7.as_ptr(),
                arg8.as_ptr(),
                arg9.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers
    for (
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
    )
{
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        let arg5 = CString::new(self.5).unwrap();
        let arg6 = CString::new(self.6).unwrap();
        let arg7 = CString::new(self.7).unwrap();
        let arg8 = CString::new(self.8).unwrap();
        let arg9 = CString::new(self.9).unwrap();
        let arg10 = CString::new(self.10).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                arg5.as_ptr(),
                arg6.as_ptr(),
                arg7.as_ptr(),
                arg8.as_ptr(),
                arg9.as_ptr(),
                arg10.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}

impl StructMembers
    for (
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
        &str,
    )
{
    fn define(self, name: Option<&str>) -> Result<RClass, Error> {
        let name = name.map(|n| CString::new(n).unwrap());
        let arg0 = CString::new(self.0).unwrap();
        let arg1 = CString::new(self.1).unwrap();
        let arg2 = CString::new(self.2).unwrap();
        let arg3 = CString::new(self.3).unwrap();
        let arg4 = CString::new(self.4).unwrap();
        let arg5 = CString::new(self.5).unwrap();
        let arg6 = CString::new(self.6).unwrap();
        let arg7 = CString::new(self.7).unwrap();
        let arg8 = CString::new(self.8).unwrap();
        let arg9 = CString::new(self.9).unwrap();
        let arg10 = CString::new(self.10).unwrap();
        let arg11 = CString::new(self.11).unwrap();
        protect(|| unsafe {
            RClass::from_rb_value_unchecked(rb_struct_define(
                name.as_ref().map(|n| n.as_ptr()).unwrap_or_else(null),
                arg0.as_ptr(),
                arg1.as_ptr(),
                arg2.as_ptr(),
                arg3.as_ptr(),
                arg4.as_ptr(),
                arg5.as_ptr(),
                arg6.as_ptr(),
                arg7.as_ptr(),
                arg8.as_ptr(),
                arg9.as_ptr(),
                arg10.as_ptr(),
                arg11.as_ptr(),
                null::<c_char>(),
            ))
        })
    }
}