Skip to content

Commit 19e9e4e

Browse files
committed
Add unstable_static_encoding_str feature
1 parent 10b7550 commit 19e9e4e

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

objc2-encode/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ readme = "README.md"
1717
repository = "https://github.com/madsmtm/objc2"
1818
documentation = "https://docs.rs/objc2-encode/"
1919
license = "MIT"
20+
21+
[features]
22+
# Requires the `generic_const_exprs` feature
23+
unstable_static_encoding_str = []

objc2-encode/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#![deny(unsafe_op_in_unsafe_fn)]
1111
// Update in Cargo.toml as well.
1212
#![doc(html_root_url = "https://docs.rs/objc2-encode/1.1.0")]
13+
#![cfg_attr(feature = "unstable_static_encoding_str", allow(incomplete_features))]
14+
#![cfg_attr(feature = "unstable_static_encoding_str", feature(generic_const_exprs))]
1315

1416
#[cfg(doctest)]
1517
#[doc = include_str!("../README.md")]
@@ -21,9 +23,11 @@ extern crate alloc;
2123
mod encode;
2224
mod encoding;
2325
mod parse;
24-
#[allow(dead_code)]
26+
#[cfg_attr(not(feature = "unstable_static_encoding_str"), allow(dead_code))]
2527
mod static_encoding_str;
2628
mod static_int_str;
2729

2830
pub use self::encode::{Encode, EncodeArguments, RefEncode};
2931
pub use self::encoding::Encoding;
32+
#[cfg(feature = "unstable_static_encoding_str")]
33+
pub use self::static_encoding_str::EncodingHelper;

objc2-encode/src/static_encoding_str.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use super::static_int_str;
2+
#[cfg(feature = "unstable_static_encoding_str")]
3+
use super::Encode;
24
use super::Encoding;
35

46
impl<'a> Encoding<'a> {
5-
const fn end_str_len(self) -> usize {
7+
#[doc(hidden)]
8+
pub const fn end_str_len(self) -> usize {
69
use Encoding::*;
710

811
match self {
@@ -26,7 +29,8 @@ impl<'a> Encoding<'a> {
2629
}
2730
}
2831

29-
const fn get_str_array<const LEN: usize>(self) -> [u8; LEN] {
32+
#[doc(hidden)]
33+
pub const fn get_str_array<const LEN: usize>(self) -> [u8; LEN] {
3034
use Encoding::*;
3135

3236
let mut res: [u8; LEN] = [0; LEN];
@@ -156,16 +160,47 @@ impl<'a> Encoding<'a> {
156160
res
157161
}
158162

159-
const fn end_cstr_len(self) -> usize {
163+
#[doc(hidden)]
164+
pub const fn end_cstr_len(self) -> usize {
160165
self.end_str_len() + 1
161166
}
162167

163-
const fn get_cstr_array<const RES: usize>(self) -> [u8; RES] {
168+
#[doc(hidden)]
169+
pub const fn get_cstr_array<const RES: usize>(self) -> [u8; RES] {
164170
// Contains nul byte at the end
165171
self.get_str_array()
166172
}
167173
}
168174

175+
/// Workaround since we can't specify the correct `where` bound on `Encode`.
176+
#[cfg(feature = "unstable_static_encoding_str")]
177+
pub struct EncodingHelper<T>(T);
178+
179+
#[cfg(feature = "unstable_static_encoding_str")]
180+
impl<T: super::Encode> EncodingHelper<T>
181+
where
182+
[u8; T::ENCODING.end_cstr_len()]: Sized,
183+
{
184+
#[doc(hidden)]
185+
const __ENCODING_CSTR_BYTES: [u8; T::ENCODING.end_cstr_len()] = T::ENCODING.get_cstr_array();
186+
187+
/// TODO
188+
pub const ENCODING_CSTR: *const u8 = Self::__ENCODING_CSTR_BYTES.as_ptr();
189+
}
190+
191+
#[cfg(feature = "unstable_static_encoding_str")]
192+
impl<T: Encode> EncodingHelper<T>
193+
where
194+
[u8; T::ENCODING.end_str_len()]: Sized,
195+
{
196+
#[doc(hidden)]
197+
const __ENCODING_STR_BYTES: [u8; T::ENCODING.end_str_len()] = T::ENCODING.get_str_array();
198+
199+
/// TODO
200+
pub const ENCODING_STR: &'static str =
201+
unsafe { core::mem::transmute::<&[u8], &str>(&Self::__ENCODING_STR_BYTES) };
202+
}
203+
169204
#[cfg(test)]
170205
mod tests {
171206
use super::Encoding;

0 commit comments

Comments
 (0)