diff --git a/Cargo.lock b/Cargo.lock index 765696e3..c83caf7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,18 +4,18 @@ version = 3 [[package]] name = "aho-corasick" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" dependencies = [ "memchr", ] [[package]] name = "bindgen" -version = "0.62.0" +version = "0.66.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6720a8b7b2d39dd533285ed438d458f65b31b5c257e6ac7bb3d7e82844dd722" +checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" dependencies = [ "bitflags", "cexpr", @@ -28,14 +28,14 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 1.0.109", + "syn", ] [[package]] name = "bitflags" -version = "1.3.2" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "bytes" @@ -122,7 +122,7 @@ dependencies = [ "magnus", "proc-macro2", "quote", - "syn 2.0.27", + "syn", ] [[package]] @@ -173,18 +173,18 @@ dependencies = [ [[package]] name = "rb-sys" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "939fb78db3e4f26665c1d4c7b91ca66d3578335a19aba552d4a6445811d07072" +checksum = "a57240b308b155b09dce81e32829966a99f52d1088b45957e4283e526c5317a1" dependencies = [ "rb-sys-build", ] [[package]] name = "rb-sys-build" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "335a95eb0420d52fa94ef12019df3c2c250c6b19cbb3c60bd05cb7e9c362072c" +checksum = "f24ce877a4c5d07f06f6aa6fec3ac95e4b357b9f73b0f5445d8cbb7266d410e8" dependencies = [ "bindgen", "lazy_static", @@ -192,7 +192,7 @@ dependencies = [ "quote", "regex", "shell-words", - "syn 1.0.109", + "syn", ] [[package]] @@ -203,9 +203,9 @@ checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb" [[package]] name = "regex" -version = "1.9.1" +version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" +checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" dependencies = [ "aho-corasick", "memchr", @@ -215,9 +215,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.3" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310" +checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" dependencies = [ "aho-corasick", "memchr", @@ -256,20 +256,9 @@ checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" [[package]] name = "syn" -version = "1.0.109" +version = "2.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "syn" -version = "2.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" +checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 2b453663..90e45187 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,14 +27,15 @@ deprecated-send-sync-value = [] [dependencies] bytes = { version = "1", optional = true } magnus-macros = { version = "0.6.0", path = "magnus-macros" } -rb-sys = { version = "0.9.77", default-features = false, features = ["bindgen-rbimpls", "bindgen-deprecated-types"] } +rb-sys = { version = "0.9.81", default-features = false, features = ["bindgen-rbimpls", "bindgen-deprecated-types", "stable-api"] } seq-macro = "0.3" [dev-dependencies] magnus = { path = ".", features = ["embed", "rb-sys"] } +rb-sys = { version = "*", default-features = false, features = ["stable-api-compiled-fallback"] } [build-dependencies] -rb-sys-env = "0.1.1" +rb-sys-env = "0.1.2" [lib] doc-scrape-examples = false diff --git a/examples/complete_object/ext/temperature/Cargo.lock b/examples/complete_object/ext/temperature/Cargo.lock index e2013eee..509219da 100644 --- a/examples/complete_object/ext/temperature/Cargo.lock +++ b/examples/complete_object/ext/temperature/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.62.0" +version = "0.66.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6720a8b7b2d39dd533285ed438d458f65b31b5c257e6ac7bb3d7e82844dd722" +checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" dependencies = [ "bitflags", "cexpr", @@ -28,14 +28,14 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 1.0.109", + "syn", ] [[package]] name = "bitflags" -version = "1.3.2" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "cexpr" @@ -113,7 +113,7 @@ version = "0.6.0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn", ] [[package]] @@ -164,18 +164,18 @@ dependencies = [ [[package]] name = "rb-sys" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "939fb78db3e4f26665c1d4c7b91ca66d3578335a19aba552d4a6445811d07072" +checksum = "a57240b308b155b09dce81e32829966a99f52d1088b45957e4283e526c5317a1" dependencies = [ "rb-sys-build", ] [[package]] name = "rb-sys-build" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "335a95eb0420d52fa94ef12019df3c2c250c6b19cbb3c60bd05cb7e9c362072c" +checksum = "f24ce877a4c5d07f06f6aa6fec3ac95e4b357b9f73b0f5445d8cbb7266d410e8" dependencies = [ "bindgen", "lazy_static", @@ -183,7 +183,7 @@ dependencies = [ "quote", "regex", "shell-words", - "syn 1.0.109", + "syn", ] [[package]] @@ -245,17 +245,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.27" @@ -272,6 +261,7 @@ name = "temperature" version = "0.1.0" dependencies = [ "magnus", + "rb-sys", ] [[package]] diff --git a/examples/complete_object/ext/temperature/Cargo.toml b/examples/complete_object/ext/temperature/Cargo.toml index 6b54a6ca..8ff51bdb 100644 --- a/examples/complete_object/ext/temperature/Cargo.toml +++ b/examples/complete_object/ext/temperature/Cargo.toml @@ -8,3 +8,6 @@ crate-type = ["cdylib"] [dependencies] magnus = { path = "../../../.." } +# enable rb-sys feature to test against Ruby head. This is only needed if you +# want to work with the unreleased, in-development, next version of Ruby +rb-sys = { version = "*", default-features = false, features = ["stable-api-compiled-fallback"] } diff --git a/examples/custom_exception_ruby/ext/ahriman/Cargo.lock b/examples/custom_exception_ruby/ext/ahriman/Cargo.lock index af0c6a6d..db45ab3e 100644 --- a/examples/custom_exception_ruby/ext/ahriman/Cargo.lock +++ b/examples/custom_exception_ruby/ext/ahriman/Cargo.lock @@ -16,13 +16,14 @@ name = "ahriman" version = "0.1.0" dependencies = [ "magnus", + "rb-sys", ] [[package]] name = "bindgen" -version = "0.62.0" +version = "0.66.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6720a8b7b2d39dd533285ed438d458f65b31b5c257e6ac7bb3d7e82844dd722" +checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" dependencies = [ "bitflags", "cexpr", @@ -35,14 +36,14 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 1.0.109", + "syn", ] [[package]] name = "bitflags" -version = "1.3.2" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "cexpr" @@ -120,7 +121,7 @@ version = "0.6.0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn", ] [[package]] @@ -171,18 +172,18 @@ dependencies = [ [[package]] name = "rb-sys" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "939fb78db3e4f26665c1d4c7b91ca66d3578335a19aba552d4a6445811d07072" +checksum = "a57240b308b155b09dce81e32829966a99f52d1088b45957e4283e526c5317a1" dependencies = [ "rb-sys-build", ] [[package]] name = "rb-sys-build" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "335a95eb0420d52fa94ef12019df3c2c250c6b19cbb3c60bd05cb7e9c362072c" +checksum = "f24ce877a4c5d07f06f6aa6fec3ac95e4b357b9f73b0f5445d8cbb7266d410e8" dependencies = [ "bindgen", "lazy_static", @@ -190,7 +191,7 @@ dependencies = [ "quote", "regex", "shell-words", - "syn 1.0.109", + "syn", ] [[package]] @@ -252,17 +253,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.27" diff --git a/examples/custom_exception_ruby/ext/ahriman/Cargo.toml b/examples/custom_exception_ruby/ext/ahriman/Cargo.toml index 9b5b48f3..99a6eaf6 100644 --- a/examples/custom_exception_ruby/ext/ahriman/Cargo.toml +++ b/examples/custom_exception_ruby/ext/ahriman/Cargo.toml @@ -8,3 +8,6 @@ crate-type = ["cdylib"] [dependencies] magnus = { path = "../../../.." } +# enable rb-sys feature to test against Ruby head. This is only needed if you +# want to work with the unreleased, in-development, next version of Ruby +rb-sys = { version = "*", default-features = false, features = ["stable-api-compiled-fallback"] } diff --git a/examples/custom_exception_rust/ext/ahriman/Cargo.lock b/examples/custom_exception_rust/ext/ahriman/Cargo.lock index af0c6a6d..db45ab3e 100644 --- a/examples/custom_exception_rust/ext/ahriman/Cargo.lock +++ b/examples/custom_exception_rust/ext/ahriman/Cargo.lock @@ -16,13 +16,14 @@ name = "ahriman" version = "0.1.0" dependencies = [ "magnus", + "rb-sys", ] [[package]] name = "bindgen" -version = "0.62.0" +version = "0.66.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6720a8b7b2d39dd533285ed438d458f65b31b5c257e6ac7bb3d7e82844dd722" +checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" dependencies = [ "bitflags", "cexpr", @@ -35,14 +36,14 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 1.0.109", + "syn", ] [[package]] name = "bitflags" -version = "1.3.2" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "cexpr" @@ -120,7 +121,7 @@ version = "0.6.0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn", ] [[package]] @@ -171,18 +172,18 @@ dependencies = [ [[package]] name = "rb-sys" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "939fb78db3e4f26665c1d4c7b91ca66d3578335a19aba552d4a6445811d07072" +checksum = "a57240b308b155b09dce81e32829966a99f52d1088b45957e4283e526c5317a1" dependencies = [ "rb-sys-build", ] [[package]] name = "rb-sys-build" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "335a95eb0420d52fa94ef12019df3c2c250c6b19cbb3c60bd05cb7e9c362072c" +checksum = "f24ce877a4c5d07f06f6aa6fec3ac95e4b357b9f73b0f5445d8cbb7266d410e8" dependencies = [ "bindgen", "lazy_static", @@ -190,7 +191,7 @@ dependencies = [ "quote", "regex", "shell-words", - "syn 1.0.109", + "syn", ] [[package]] @@ -252,17 +253,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.27" diff --git a/examples/custom_exception_rust/ext/ahriman/Cargo.toml b/examples/custom_exception_rust/ext/ahriman/Cargo.toml index 9b5b48f3..99a6eaf6 100644 --- a/examples/custom_exception_rust/ext/ahriman/Cargo.toml +++ b/examples/custom_exception_rust/ext/ahriman/Cargo.toml @@ -8,3 +8,6 @@ crate-type = ["cdylib"] [dependencies] magnus = { path = "../../../.." } +# enable rb-sys feature to test against Ruby head. This is only needed if you +# want to work with the unreleased, in-development, next version of Ruby +rb-sys = { version = "*", default-features = false, features = ["stable-api-compiled-fallback"] } diff --git a/examples/rust_blank/ext/rust_blank/Cargo.lock b/examples/rust_blank/ext/rust_blank/Cargo.lock index ec6ee357..1045fa00 100644 --- a/examples/rust_blank/ext/rust_blank/Cargo.lock +++ b/examples/rust_blank/ext/rust_blank/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.62.0" +version = "0.66.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6720a8b7b2d39dd533285ed438d458f65b31b5c257e6ac7bb3d7e82844dd722" +checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" dependencies = [ "bitflags", "cexpr", @@ -28,14 +28,14 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 1.0.109", + "syn", ] [[package]] name = "bitflags" -version = "1.3.2" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "cexpr" @@ -113,7 +113,7 @@ version = "0.6.0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.27", + "syn", ] [[package]] @@ -164,18 +164,18 @@ dependencies = [ [[package]] name = "rb-sys" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "939fb78db3e4f26665c1d4c7b91ca66d3578335a19aba552d4a6445811d07072" +checksum = "a57240b308b155b09dce81e32829966a99f52d1088b45957e4283e526c5317a1" dependencies = [ "rb-sys-build", ] [[package]] name = "rb-sys-build" -version = "0.9.79" +version = "0.9.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "335a95eb0420d52fa94ef12019df3c2c250c6b19cbb3c60bd05cb7e9c362072c" +checksum = "f24ce877a4c5d07f06f6aa6fec3ac95e4b357b9f73b0f5445d8cbb7266d410e8" dependencies = [ "bindgen", "lazy_static", @@ -183,7 +183,7 @@ dependencies = [ "quote", "regex", "shell-words", - "syn 1.0.109", + "syn", ] [[package]] @@ -226,6 +226,7 @@ name = "rust_blank" version = "0.1.0" dependencies = [ "magnus", + "rb-sys", ] [[package]] @@ -252,17 +253,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.27" diff --git a/examples/rust_blank/ext/rust_blank/Cargo.toml b/examples/rust_blank/ext/rust_blank/Cargo.toml index 2c515353..657a5a86 100644 --- a/examples/rust_blank/ext/rust_blank/Cargo.toml +++ b/examples/rust_blank/ext/rust_blank/Cargo.toml @@ -8,3 +8,6 @@ crate-type = ["cdylib"] [dependencies] magnus = { path = "../../../.." } +# enable rb-sys feature to test against Ruby head. This is only needed if you +# want to work with the unreleased, in-development, next version of Ruby +rb-sys = { version = "*", default-features = false, features = ["stable-api-compiled-fallback"] } diff --git a/src/r_array.rs b/src/r_array.rs index 0676fb47..61318a89 100644 --- a/src/r_array.rs +++ b/src/r_array.rs @@ -1,16 +1,12 @@ -use std::{cmp::Ordering, fmt, os::raw::c_long, ptr::NonNull, slice}; +use std::{cmp::Ordering, fmt, os::raw::c_long, slice}; -#[cfg(ruby_gte_3_0)] -use rb_sys::ruby_rarray_consts::RARRAY_EMBED_LEN_SHIFT; -#[cfg(ruby_lt_3_0)] -use rb_sys::ruby_rarray_flags::RARRAY_EMBED_LEN_SHIFT; use rb_sys::{ self, rb_ary_assoc, rb_ary_cat, rb_ary_clear, rb_ary_cmp, rb_ary_concat, rb_ary_delete, rb_ary_delete_at, rb_ary_entry, rb_ary_includes, rb_ary_join, rb_ary_new, rb_ary_new_capa, rb_ary_new_from_values, rb_ary_plus, rb_ary_pop, rb_ary_push, rb_ary_rassoc, rb_ary_replace, rb_ary_resize, rb_ary_reverse, rb_ary_rotate, rb_ary_shared_with_p, rb_ary_shift, rb_ary_sort_bang, rb_ary_store, rb_ary_subseq, rb_ary_to_ary, rb_ary_unshift, - rb_check_array_type, ruby_rarray_flags, ruby_value_type, VALUE, + rb_check_array_type, ruby_value_type, RARRAY_CONST_PTR, RARRAY_LEN, VALUE, }; use crate::{ @@ -217,11 +213,6 @@ impl RArray { Self(NonZeroValue::new_unchecked(Value::new(val))) } - fn as_internal(self) -> NonNull { - // safe as inner value is NonZero - unsafe { NonNull::new_unchecked(self.0.get().as_rb_value() as *mut _) } - } - /// Create a new empty `RArray`. /// /// # Panics @@ -352,18 +343,7 @@ impl RArray { /// ``` pub fn len(self) -> usize { debug_assert_value!(self); - unsafe { - let r_basic = self.r_basic_unchecked(); - let flags = r_basic.as_ref().flags; - if (flags & ruby_rarray_flags::RARRAY_EMBED_FLAG as VALUE) != 0 { - let len = (flags >> RARRAY_EMBED_LEN_SHIFT as VALUE) - & (ruby_rarray_flags::RARRAY_EMBED_LEN_MASK as VALUE - >> RARRAY_EMBED_LEN_SHIFT as VALUE); - len.try_into().unwrap() - } else { - self.as_internal().as_ref().as_.heap.len.try_into().unwrap() - } - } + unsafe { RARRAY_LEN(self.as_rb_value()) as _ } } /// Return whether self contains any entries or not. @@ -876,20 +856,10 @@ impl RArray { 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 & ruby_rarray_flags::RARRAY_EMBED_FLAG as VALUE) != 0 { - let len = (flags >> RARRAY_EMBED_LEN_SHIFT as VALUE) - & (ruby_rarray_flags::RARRAY_EMBED_LEN_MASK as VALUE - >> RARRAY_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) - } + slice::from_raw_parts( + RARRAY_CONST_PTR(self.as_rb_value()) as *const Value, + RARRAY_LEN(self.as_rb_value()) as usize, + ) } /// Convert `self` to a Rust vector of `T`s. Errors if converting any diff --git a/src/r_string.rs b/src/r_string.rs index a767dc27..ed333102 100644 --- a/src/r_string.rs +++ b/src/r_string.rs @@ -9,23 +9,19 @@ use std::{ mem::transmute, os::raw::{c_char, c_long}, path::{Path, PathBuf}, - ptr::{self, NonNull}, + ptr::{self}, slice, str, }; #[cfg(ruby_gte_3_0)] use rb_sys::rb_str_to_interned_str; -#[cfg(all(ruby_gte_3_0, ruby_lt_3_2))] -use rb_sys::ruby_rstring_consts::RSTRING_EMBED_LEN_SHIFT; -#[cfg(ruby_lt_3_0)] -use rb_sys::ruby_rstring_flags::RSTRING_EMBED_LEN_SHIFT; use rb_sys::{ self, rb_enc_str_coderange, rb_enc_str_new, rb_str_buf_append, rb_str_buf_new, rb_str_capacity, rb_str_cat, rb_str_cmp, rb_str_comparable, rb_str_conv_enc, rb_str_drop_bytes, rb_str_dump, rb_str_ellipsize, rb_str_new, rb_str_new_frozen, rb_str_new_shared, rb_str_offset, rb_str_plus, rb_str_replace, rb_str_scrub, rb_str_shared_replace, rb_str_split, rb_str_strlen, rb_str_times, rb_str_to_str, rb_str_update, rb_utf8_str_new, rb_utf8_str_new_static, ruby_coderange_type, - ruby_rstring_flags, ruby_value_type, VALUE, + ruby_rstring_flags, ruby_value_type, RSTRING_LEN, RSTRING_PTR, VALUE, }; use crate::{ @@ -315,11 +311,6 @@ impl RString { Self(NonZeroValue::new_unchecked(Value::new(val))) } - fn as_internal(self) -> NonNull { - // safe as inner value is NonZero - unsafe { NonNull::new_unchecked(self.0.get().as_rb_value() as *mut _) } - } - /// Create a new Ruby string from the Rust string `s`. /// /// The encoding of the Ruby string will be UTF-8. @@ -625,25 +616,11 @@ impl RString { } unsafe fn as_slice_unconstrained<'a>(self) -> &'a [u8] { - #[cfg(ruby_gte_3_1)] - unsafe fn embedded_ary_ptr(rstring: RString) -> *const u8 { - &rstring.as_internal().as_ref().as_.embed.ary as *const _ as *const u8 - } - - #[cfg(ruby_lt_3_1)] - unsafe fn embedded_ary_ptr(rstring: RString) -> *const u8 { - &rstring.as_internal().as_ref().as_.ary as *const _ as *const u8 - } - debug_assert_value!(self); - let r_basic = self.r_basic_unchecked(); - let f = r_basic.as_ref().flags; - if (f & ruby_rstring_flags::RSTRING_NOEMBED as VALUE) != 0 { - let h = self.as_internal().as_ref().as_.heap; - slice::from_raw_parts(h.ptr as *const u8, self.len()) - } else { - slice::from_raw_parts(embedded_ary_ptr(self), self.len()) - } + slice::from_raw_parts( + RSTRING_PTR(self.as_rb_value()) as *const u8, + RSTRING_LEN(self.as_rb_value()) as _, + ) } /// Return an iterator over `self`'s codepoints. @@ -1490,38 +1467,9 @@ impl RString { /// let s = RString::new("🦀 Hello, Ferris"); /// assert_eq!(s.len(), 18); /// ``` - #[cfg(ruby_gte_3_3)] pub fn len(self) -> usize { debug_assert_value!(self); - unsafe { self.as_internal().as_ref().len as usize } - } - - /// Returns the number of bytes in `self`. - /// - /// See also [`length`](RString::length). - /// - /// # Examples - /// - /// ``` - /// use magnus::RString; - /// # let _cleanup = unsafe { magnus::embed::init() }; - /// - /// let s = RString::new("🦀 Hello, Ferris"); - /// assert_eq!(s.len(), 18); - /// ``` - #[cfg(ruby_lte_3_2)] - pub fn len(self) -> usize { - debug_assert_value!(self); - unsafe { - let r_basic = self.r_basic_unchecked(); - let f = r_basic.as_ref().flags; - if (f & ruby_rstring_flags::RSTRING_NOEMBED as VALUE) != 0 { - let h = self.as_internal().as_ref().as_.heap; - h.len as usize - } else { - embed_len(self, f) as usize - } - } + unsafe { RSTRING_LEN(self.as_rb_value()) as usize } } /// Returns the number of characters in `self`. @@ -1664,18 +1612,6 @@ impl RString { } } -#[cfg(ruby_3_2)] -unsafe fn embed_len(value: RString, _: VALUE) -> c_long { - value.as_internal().as_ref().as_.embed.len -} - -#[cfg(ruby_lt_3_2)] -unsafe fn embed_len(_: RString, mut f: VALUE) -> VALUE { - f &= ruby_rstring_flags::RSTRING_EMBED_LEN_MASK as VALUE; - f >>= RSTRING_EMBED_LEN_SHIFT as VALUE; - f -} - impl fmt::Display for RString { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", unsafe { self.to_s_infallible() })