Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 894fc06

Browse files
chancancodewycats
authored andcommitted
Use *const T and *mut T more consistently
Some context in rust-lang/rfcs#68
1 parent 8281778 commit 894fc06

File tree

7 files changed

+42
-23
lines changed

7 files changed

+42
-23
lines changed

crates/libcruby-sys/src/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ pub fn check_version() {
1616
}
1717
}
1818

19-
pub type void_ptr = *const libc::c_void;
19+
pub type void = libc::c_void;
20+
pub type c_func = *const void;
2021
pub type c_string = *const libc::c_char;
2122
// pub type c_func = extern "C" fn(...);
2223

2324
#[repr(C)]
2425
#[derive(Copy, Clone, Debug)]
25-
pub struct ID(void_ptr);
26+
pub struct ID(*mut void);
2627

2728
#[repr(C)]
2829
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
29-
pub struct VALUE(void_ptr);
30+
pub struct VALUE(*mut void);
3031

3132
#[repr(C)]
3233
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
@@ -78,7 +79,10 @@ extern "C" {
7879
pub fn RARRAY_LEN(array: VALUE) -> isize;
7980

8081
#[link_name = "HELIX_RARRAY_PTR"]
81-
pub fn RARRAY_PTR(array: VALUE) -> void_ptr;
82+
pub fn RARRAY_PTR(array: VALUE) -> *mut VALUE;
83+
84+
#[link_name = "HELIX_RARRAY_CONST_PTR"]
85+
pub fn RARRAY_CONST_PTR(array: VALUE) -> *const VALUE;
8286

8387
#[link_name = "HELIX_RB_TYPE_P"]
8488
pub fn RB_TYPE_P(val: VALUE, rb_type: isize) -> bool;
@@ -150,22 +154,22 @@ extern "C" {
150154
pub fn rb_define_class(name: c_string, superclass: VALUE) -> VALUE;
151155
pub fn rb_define_class_under(namespace: VALUE, name: c_string, superclass: VALUE) -> VALUE;
152156
pub fn rb_define_alloc_func(klass: VALUE, func: extern "C" fn(klass: VALUE) -> VALUE);
153-
pub fn rb_define_method(class: VALUE, name: c_string, func: void_ptr, arity: isize);
154-
pub fn rb_define_singleton_method(class: VALUE, name: c_string, func: void_ptr, arity: isize);
157+
pub fn rb_define_method(class: VALUE, name: c_string, func: c_func, arity: isize);
158+
pub fn rb_define_singleton_method(class: VALUE, name: c_string, func: c_func, arity: isize);
155159
pub fn rb_inspect(value: VALUE) -> VALUE;
156160
pub fn rb_intern(string: c_string) -> ID;
157161
pub fn rb_jump_tag(state: RubyException) -> !;
158-
pub fn rb_protect(try: extern "C" fn(v: void_ptr) -> VALUE,
159-
arg: void_ptr,
162+
pub fn rb_protect(try: extern "C" fn(v: *mut void) -> VALUE,
163+
arg: *mut void,
160164
state: *mut RubyException)
161165
-> VALUE;
162166

163167
#[link_name = "HELIX_Data_Wrap_Struct"]
164-
pub fn Data_Wrap_Struct(klass: VALUE, mark: extern "C" fn(void_ptr), free: extern "C" fn(void_ptr), data: void_ptr) -> VALUE;
168+
pub fn Data_Wrap_Struct(klass: VALUE, mark: extern "C" fn(*mut void), free: extern "C" fn(*mut void), data: *mut void) -> VALUE;
165169

166170
#[link_name = "HELIX_Data_Get_Struct_Value"]
167-
pub fn Data_Get_Struct_Value(obj: VALUE) -> void_ptr;
171+
pub fn Data_Get_Struct_Value(obj: VALUE) -> *mut void;
168172

169173
#[link_name = "HELIX_Data_Set_Struct_Value"]
170-
pub fn Data_Set_Struct_Value(obj: VALUE, data: void_ptr);
174+
pub fn Data_Set_Struct_Value(obj: VALUE, data: *mut void);
171175
}

ruby/ext/helix_runtime/native/helix_runtime.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ void* HELIX_RARRAY_PTR(VALUE array) {
2828
return RARRAY_PTR(array);
2929
}
3030

31+
const void* HELIX_RARRAY_CONST_PTR(VALUE array) {
32+
return RARRAY_CONST_PTR(array);
33+
}
34+
3135
bool HELIX_RB_TYPE_P(VALUE v, int type) {
3236
return RB_TYPE_P(v, type);
3337
}

ruby/ext/helix_runtime/native/helix_runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ HELIX_EXTERN const char* HELIX_RSTRING_PTR(VALUE string);
4949

5050
HELIX_EXTERN long HELIX_RARRAY_LEN(VALUE array);
5151
HELIX_EXTERN void* HELIX_RARRAY_PTR(VALUE array);
52+
HELIX_EXTERN const void* HELIX_RARRAY_CONST_PTR(VALUE array);
5253

5354
HELIX_EXTERN bool HELIX_RB_TYPE_P(VALUE v, int type);
5455
HELIX_EXTERN int HELIX_TYPE(VALUE v);

ruby/spec/helix_runtime_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ module TYPES
7070
expect(Dummy.RARRAY_PTR(arr)).to eq(Dummy::RARRAY_PTR(arr))
7171
end
7272

73+
it 'exports the RARRAY_CONST_PTR macro' do
74+
arr = [1,2,3,4,5]
75+
expect(Dummy.RARRAY_CONST_PTR([1,2,3,4,5])).to_not eq(Dummy::RARRAY_CONST_PTR([1,2,3,4,5]))
76+
expect(Dummy.RARRAY_CONST_PTR(arr)).to eq(Dummy::RARRAY_CONST_PTR(arr))
77+
end
78+
7379
describe 'coercions' do
7480
it "(INT2FIX)" do
7581
expect(Dummy.INT2FIX(10)).to eq(10)

ruby/spec/support/dummy/ext/dummy/dummy.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ static VALUE TEST_RARRAY_PTR(VALUE _self, VALUE val) {
3131
return SIZET2NUM((uintptr_t)HELIX_RARRAY_PTR(val));
3232
}
3333

34+
static VALUE TEST_RB_RARRAY_CONST_PTR(VALUE _self, VALUE val) {
35+
return SIZET2NUM((uintptr_t)RARRAY_CONST_PTR(val));
36+
}
37+
38+
static VALUE TEST_RARRAY_CONST_PTR(VALUE _self, VALUE val) {
39+
return SIZET2NUM((uintptr_t)HELIX_RARRAY_CONST_PTR(val));
40+
}
41+
3442
static VALUE TEST_RB_TYPE_P(VALUE _self, VALUE val, VALUE type) {
3543
int result = HELIX_RB_TYPE_P(val, FIX2INT(type));
3644
return result ? Qtrue : Qfalse;
@@ -176,6 +184,8 @@ void Init_dummy() {
176184
EXPORT_FUNC(RARRAY_LEN, 1);
177185
EXPORT_FUNC(RARRAY_PTR, 1);
178186
EXPORT_RUBY_FUNC(RARRAY_PTR, 1);
187+
EXPORT_FUNC(RARRAY_CONST_PTR, 1);
188+
EXPORT_RUBY_FUNC(RARRAY_CONST_PTR, 1);
179189
EXPORT_FUNC(RB_TYPE_P, 2);
180190
EXPORT_FUNC(TYPE, 1);
181191
EXPORT_FUNC(INT2FIX, 1);

src/class_definition.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
use libc;
2-
use { Class, sys };
3-
4-
#[allow(non_camel_case_types)]
5-
type c_string = *const libc::c_char;
6-
7-
#[allow(non_camel_case_types)]
8-
type void_ptr = *const libc::c_void;
1+
use { Class };
2+
use sys::{self, c_string, c_func};
93

104
pub struct MethodSpecification {
115
name: c_string,
12-
function: void_ptr,
6+
function: c_func,
137
arity: isize,
148
}
159

@@ -19,11 +13,11 @@ pub enum MethodDefinition {
1913
}
2014

2115
impl MethodDefinition {
22-
pub fn class(name: c_string, function: void_ptr, arity: isize) -> MethodDefinition {
16+
pub fn class(name: c_string, function: c_func, arity: isize) -> MethodDefinition {
2317
MethodDefinition::Class(MethodSpecification { name: name, function: function, arity: arity })
2418
}
2519

26-
pub fn instance(name: c_string, function: void_ptr, arity: isize) -> MethodDefinition {
20+
pub fn instance(name: c_string, function: c_func, arity: isize) -> MethodDefinition {
2721
MethodDefinition::Instance(MethodSpecification { name: name, function: function, arity: arity })
2822
}
2923
}

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ macro_rules! impl_struct_to_rust {
407407
use ::std::ffi::{CStr, CString};
408408

409409
if unsafe { __HELIX_ID == ::std::mem::transmute(sys::rb_obj_class(self)) } {
410-
if unsafe { $crate::sys::Data_Get_Struct_Value(self) == ::std::ptr::null() } {
410+
if unsafe { $crate::sys::Data_Get_Struct_Value(self) == ::std::ptr::null_mut() } {
411411
Err(CString::new(format!("Uninitialized {}", $crate::inspect(unsafe { sys::rb_obj_class(self) }))).unwrap())
412412
} else {
413413
Ok(unsafe { CheckedValue::new(self) })

0 commit comments

Comments
 (0)