Skip to content

Commit b354d5e

Browse files
committed
Add unstable-static-encoding-str feature
1 parent 3d1a729 commit b354d5e

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

objc2-encode/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ repository = "https://github.com/madsmtm/objc2"
1818
documentation = "https://docs.rs/objc2-encode/"
1919
license = "MIT"
2020

21+
[features]
22+
# Requires the `generic_const_exprs` feature
23+
unstable-static-encoding-str = []
24+
2125
[package.metadata.docs.rs]
2226
default-target = "x86_64-apple-darwin"

objc2-encode/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
#![deny(unsafe_op_in_unsafe_fn)]
9393
// Update in Cargo.toml as well.
9494
#![doc(html_root_url = "https://docs.rs/objc2-encode/2.0.0-beta.2")]
95+
#![cfg_attr(feature = "unstable-static-encoding-str", allow(incomplete_features))]
96+
#![cfg_attr(feature = "unstable-static-encoding-str", feature(generic_const_exprs))]
9597

9698
#[cfg(doctest)]
9799
#[doc = include_str!("../README.md")]
@@ -108,8 +110,10 @@ mod encoding;
108110
mod parse;
109111

110112
// Will be used at some point when generic constants are available
111-
#[allow(dead_code)]
113+
#[cfg_attr(not(feature = "unstable-static-encoding-str"), allow(dead_code))]
112114
mod static_str;
113115

114116
pub use self::encode::{Encode, EncodeArguments, RefEncode};
115117
pub use self::encoding::Encoding;
118+
#[cfg(feature = "unstable-static-encoding-str")]
119+
pub use self::static_str::EncodingHelper;

objc2-encode/src/static_str.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(feature = "unstable-static-encoding-str")]
2+
use super::Encode;
13
use super::Encoding;
24

35
pub(crate) const fn static_int_str_len(mut n: u128) -> usize {
@@ -41,8 +43,7 @@ pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>) -> usize {
4143

4244
match encoding {
4345
Char | Short | Int | Long | LongLong | UChar | UShort | UInt | ULong | ULongLong
44-
| Float | Double | LongDouble | Bool | Void | String | Object | Class | Sel
45-
| Unknown => 1,
46+
| Float | Double | LongDouble | Bool | Void | String | Object | Class | Sel | Unknown => 1,
4647
Block | FloatComplex | DoubleComplex | LongDoubleComplex => 2,
4748
BitField(b, _type) => 1 + static_int_str_len(b as u128),
4849
Pointer(&t) => 1 + static_encoding_str_len(t),
@@ -61,7 +62,9 @@ pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>) -> usize {
6162
}
6263
}
6364

64-
pub(crate) const fn static_encoding_str_array<const LEN: usize>(encoding: Encoding<'_>) -> [u8; LEN] {
65+
pub(crate) const fn static_encoding_str_array<const LEN: usize>(
66+
encoding: Encoding<'_>,
67+
) -> [u8; LEN] {
6568
use Encoding::*;
6669

6770
let mut res: [u8; LEN] = [0; LEN];
@@ -204,6 +207,38 @@ pub(crate) const fn static_encoding_str_array<const LEN: usize>(encoding: Encodi
204207
res
205208
}
206209

210+
/// Workaround since we can't specify the correct `where` bound on `Encode`.
211+
#[cfg(feature = "unstable-static-encoding-str")]
212+
pub struct EncodingHelper<T>(T);
213+
214+
#[cfg(feature = "unstable-static-encoding-str")]
215+
impl<T: super::Encode> EncodingHelper<T>
216+
where
217+
[u8; static_encoding_str_len(T::ENCODING) + 1]: Sized,
218+
{
219+
#[doc(hidden)]
220+
// Contains null byte at the end
221+
const __ENCODING_CSTR_BYTES: [u8; static_encoding_str_len(T::ENCODING) + 1] =
222+
static_encoding_str_array(T::ENCODING);
223+
224+
/// TODO
225+
pub const ENCODING_CSTR: *const u8 = Self::__ENCODING_CSTR_BYTES.as_ptr();
226+
}
227+
228+
#[cfg(feature = "unstable-static-encoding-str")]
229+
impl<T: Encode> EncodingHelper<T>
230+
where
231+
[u8; static_encoding_str_len(T::ENCODING)]: Sized,
232+
{
233+
#[doc(hidden)]
234+
const __ENCODING_STR_BYTES: [u8; static_encoding_str_len(T::ENCODING)] =
235+
static_encoding_str_array(T::ENCODING);
236+
237+
/// TODO
238+
pub const ENCODING_STR: &'static str =
239+
unsafe { core::mem::transmute::<&[u8], &str>(&Self::__ENCODING_STR_BYTES) };
240+
}
241+
207242
#[cfg(test)]
208243
mod tests {
209244
use super::*;

0 commit comments

Comments
 (0)