Skip to content

Commit

Permalink
Add unstable_static_encoding_str feature
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Nov 3, 2021
1 parent 10b7550 commit 19e9e4e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
4 changes: 4 additions & 0 deletions objc2-encode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ readme = "README.md"
repository = "https://github.com/madsmtm/objc2"
documentation = "https://docs.rs/objc2-encode/"
license = "MIT"

[features]
# Requires the `generic_const_exprs` feature
unstable_static_encoding_str = []
6 changes: 5 additions & 1 deletion objc2-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#![deny(unsafe_op_in_unsafe_fn)]
// Update in Cargo.toml as well.
#![doc(html_root_url = "https://docs.rs/objc2-encode/1.1.0")]
#![cfg_attr(feature = "unstable_static_encoding_str", allow(incomplete_features))]
#![cfg_attr(feature = "unstable_static_encoding_str", feature(generic_const_exprs))]

#[cfg(doctest)]
#[doc = include_str!("../README.md")]
Expand All @@ -21,9 +23,11 @@ extern crate alloc;
mod encode;
mod encoding;
mod parse;
#[allow(dead_code)]
#[cfg_attr(not(feature = "unstable_static_encoding_str"), allow(dead_code))]
mod static_encoding_str;
mod static_int_str;

pub use self::encode::{Encode, EncodeArguments, RefEncode};
pub use self::encoding::Encoding;
#[cfg(feature = "unstable_static_encoding_str")]
pub use self::static_encoding_str::EncodingHelper;
43 changes: 39 additions & 4 deletions objc2-encode/src/static_encoding_str.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use super::static_int_str;
#[cfg(feature = "unstable_static_encoding_str")]
use super::Encode;
use super::Encoding;

impl<'a> Encoding<'a> {
const fn end_str_len(self) -> usize {
#[doc(hidden)]
pub const fn end_str_len(self) -> usize {
use Encoding::*;

match self {
Expand All @@ -26,7 +29,8 @@ impl<'a> Encoding<'a> {
}
}

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

let mut res: [u8; LEN] = [0; LEN];
Expand Down Expand Up @@ -156,16 +160,47 @@ impl<'a> Encoding<'a> {
res
}

const fn end_cstr_len(self) -> usize {
#[doc(hidden)]
pub const fn end_cstr_len(self) -> usize {
self.end_str_len() + 1
}

const fn get_cstr_array<const RES: usize>(self) -> [u8; RES] {
#[doc(hidden)]
pub const fn get_cstr_array<const RES: usize>(self) -> [u8; RES] {
// Contains nul byte at the end
self.get_str_array()
}
}

/// Workaround since we can't specify the correct `where` bound on `Encode`.
#[cfg(feature = "unstable_static_encoding_str")]
pub struct EncodingHelper<T>(T);

#[cfg(feature = "unstable_static_encoding_str")]
impl<T: super::Encode> EncodingHelper<T>
where
[u8; T::ENCODING.end_cstr_len()]: Sized,
{
#[doc(hidden)]
const __ENCODING_CSTR_BYTES: [u8; T::ENCODING.end_cstr_len()] = T::ENCODING.get_cstr_array();

/// TODO
pub const ENCODING_CSTR: *const u8 = Self::__ENCODING_CSTR_BYTES.as_ptr();
}

#[cfg(feature = "unstable_static_encoding_str")]
impl<T: Encode> EncodingHelper<T>
where
[u8; T::ENCODING.end_str_len()]: Sized,
{
#[doc(hidden)]
const __ENCODING_STR_BYTES: [u8; T::ENCODING.end_str_len()] = T::ENCODING.get_str_array();

/// TODO
pub const ENCODING_STR: &'static str =
unsafe { core::mem::transmute::<&[u8], &str>(&Self::__ENCODING_STR_BYTES) };
}

#[cfg(test)]
mod tests {
use super::Encoding;
Expand Down

0 comments on commit 19e9e4e

Please sign in to comment.