Skip to content

Commit

Permalink
Hide cstr literal syntax from msrv
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Apr 13, 2024
1 parent 9e9159c commit 57dee0d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 65 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
- name: Install Rust toolchain
uses: dtolnay/[email protected] # MSRV
- name: Check (-std -alloc)
run: cargo check --lib --no-default-features
run: cargo check --no-default-features
- name: Check (-std +alloc)
run: cargo check --lib --no-default-features --features alloc
run: cargo check --no-default-features --features alloc
- name: Check (+std +alloc)
run: cargo check --lib --no-default-features --features alloc,std
run: cargo check --no-default-features --features alloc,std

tests:
name: Tests
Expand All @@ -29,7 +29,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@1.72 # MSRV
uses: dtolnay/rust-toolchain
- name: Build tests
run: cargo test --no-run
- name: Run tests
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ mod buf;

mod slice;

#[cfg(test)]
mod tests;

#[cfg(feature = "alloc")]
pub use self::buf::*;

Expand Down
61 changes: 0 additions & 61 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,64 +245,3 @@ pub mod __internal_unstable {
/// cstr8!(c"cstr with \xa0\xa1 invalid utf-8");
/// ```
const _: () = ();

#[cfg(test)]
mod tests {
use {
crate::{cstr8, CStr8},
core::ffi::CStr,
};

#[test]
fn test_cstr8_macro() {
const STR_LITERAL: &CStr8 = cstr8!("literal");
assert_eq!(STR_LITERAL, "literal");
assert_eq!(STR_LITERAL.as_bytes_with_nul(), b"literal\0");

const STR_CONSTANT: &str = "constant";
const STR_WITH_NUL: &CStr8 = cstr8!(STR_CONSTANT);
assert_eq!(STR_WITH_NUL, "constant");
assert_eq!(STR_WITH_NUL.as_bytes_with_nul(), b"constant\0");

const BYTES_CONSTANT: &[u8] = b"bytes constant";
const BYTES_WITH_NUL: &CStr8 = cstr8!(BYTES_CONSTANT);
assert_eq!(BYTES_WITH_NUL, "bytes constant");
assert_eq!(BYTES_WITH_NUL.as_bytes_with_nul(), b"bytes constant\0");

const BYTE_ARRAY_REF_LITERAL: &CStr8 = cstr8!(b"bytes literal");
assert_eq!(BYTE_ARRAY_REF_LITERAL, "bytes literal");
assert_eq!(
BYTE_ARRAY_REF_LITERAL.as_bytes_with_nul(),
b"bytes literal\0"
);

const BYTE_ARRAY_REF_CONSTANT: &[u8; 14] = b"bytes constant";
const BYTE_ARRAY_REF_WITH_NUL: &CStr8 = cstr8!(BYTE_ARRAY_REF_CONSTANT);
assert_eq!(BYTE_ARRAY_REF_WITH_NUL, "bytes constant");
assert_eq!(
BYTE_ARRAY_REF_WITH_NUL.as_bytes_with_nul(),
b"bytes constant\0"
);

const BYTE_ARRAY_LITERAL: &CStr8 = cstr8!([b'h', b'i']);
assert_eq!(BYTE_ARRAY_LITERAL, "hi");
assert_eq!(BYTE_ARRAY_LITERAL.as_bytes_with_nul(), b"hi\0");

const BYTE_ARRAY_CONSTANT: [u8; 19] = *b"byte array constant";
const BYTE_ARRAY_WITH_NUL: &CStr8 = cstr8!(BYTE_ARRAY_CONSTANT);
assert_eq!(BYTE_ARRAY_WITH_NUL, "byte array constant");
assert_eq!(
BYTE_ARRAY_WITH_NUL.as_bytes_with_nul(),
b"byte array constant\0"
);

const CSTR_LITERAL: &CStr8 = cstr8!(c"cstr literal");
assert_eq!(CSTR_LITERAL, "cstr literal");
assert_eq!(CSTR_LITERAL.as_bytes_with_nul(), b"cstr literal\0");

const CSTR_CONSTANT: &CStr = c"cstr constant";
const CSTR_UTF8: &CStr8 = cstr8!(CSTR_CONSTANT);
assert_eq!(CSTR_UTF8, "cstr constant");
assert_eq!(CSTR_UTF8.as_bytes_with_nul(), b"cstr constant\0");
}
}
57 changes: 57 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use {
crate::{cstr8, CStr8},
core::ffi::CStr,
};

#[test]
fn test_cstr8_macro() {
const STR_LITERAL: &CStr8 = cstr8!("literal");
assert_eq!(STR_LITERAL, "literal");
assert_eq!(STR_LITERAL.as_bytes_with_nul(), b"literal\0");

const STR_CONSTANT: &str = "constant";
const STR_WITH_NUL: &CStr8 = cstr8!(STR_CONSTANT);
assert_eq!(STR_WITH_NUL, "constant");
assert_eq!(STR_WITH_NUL.as_bytes_with_nul(), b"constant\0");

const BYTES_CONSTANT: &[u8] = b"bytes constant";
const BYTES_WITH_NUL: &CStr8 = cstr8!(BYTES_CONSTANT);
assert_eq!(BYTES_WITH_NUL, "bytes constant");
assert_eq!(BYTES_WITH_NUL.as_bytes_with_nul(), b"bytes constant\0");

const BYTE_ARRAY_REF_LITERAL: &CStr8 = cstr8!(b"bytes literal");
assert_eq!(BYTE_ARRAY_REF_LITERAL, "bytes literal");
assert_eq!(
BYTE_ARRAY_REF_LITERAL.as_bytes_with_nul(),
b"bytes literal\0"
);

const BYTE_ARRAY_REF_CONSTANT: &[u8; 14] = b"bytes constant";
const BYTE_ARRAY_REF_WITH_NUL: &CStr8 = cstr8!(BYTE_ARRAY_REF_CONSTANT);
assert_eq!(BYTE_ARRAY_REF_WITH_NUL, "bytes constant");
assert_eq!(
BYTE_ARRAY_REF_WITH_NUL.as_bytes_with_nul(),
b"bytes constant\0"
);

const BYTE_ARRAY_LITERAL: &CStr8 = cstr8!([b'h', b'i']);
assert_eq!(BYTE_ARRAY_LITERAL, "hi");
assert_eq!(BYTE_ARRAY_LITERAL.as_bytes_with_nul(), b"hi\0");

const BYTE_ARRAY_CONSTANT: [u8; 19] = *b"byte array constant";
const BYTE_ARRAY_WITH_NUL: &CStr8 = cstr8!(BYTE_ARRAY_CONSTANT);
assert_eq!(BYTE_ARRAY_WITH_NUL, "byte array constant");
assert_eq!(
BYTE_ARRAY_WITH_NUL.as_bytes_with_nul(),
b"byte array constant\0"
);

const CSTR_LITERAL: &CStr8 = cstr8!(c"cstr literal");
assert_eq!(CSTR_LITERAL, "cstr literal");
assert_eq!(CSTR_LITERAL.as_bytes_with_nul(), b"cstr literal\0");

const CSTR_CONSTANT: &CStr = c"cstr constant";
const CSTR_UTF8: &CStr8 = cstr8!(CSTR_CONSTANT);
assert_eq!(CSTR_UTF8, "cstr constant");
assert_eq!(CSTR_UTF8.as_bytes_with_nul(), b"cstr constant\0");
}

0 comments on commit 57dee0d

Please sign in to comment.