#string-literal #literals #optimization #c-compatible #libc #c-str #const-str

const-cstr-fork

从 Rust 字符串字面量创建静态 C 兼容字符串。https://github.com/abonander/const-cstr 的分支

1 个不稳定版本

使用旧 Rust 2015

0.2.2 2017 年 6 月 1 日

12#c-str

Download history 54/week @ 2024-03-03 70/week @ 2024-03-10 59/week @ 2024-03-17 104/week @ 2024-03-24 126/week @ 2024-03-31 47/week @ 2024-04-07 60/week @ 2024-04-14 75/week @ 2024-04-21 74/week @ 2024-04-28 68/week @ 2024-05-05 63/week @ 2024-05-12 67/week @ 2024-05-19 61/week @ 2024-05-26 50/week @ 2024-06-02 36/week @ 2024-06-09 74/week @ 2024-06-16

234 每月下载量
用于 23 个crate (4 直接)

MIT/Apache

8KB
81

const-cstr-fork

此crate是从 const-str 分支出来的,旨在在某些场景中优化性能。它可能会消失或合并回 const-cstr。对 constr-str 的作者为原始设计表示感谢。

从 Rust 字符串字面量创建静态 C 兼容字符串。

用法

Cargo.toml

[dependencies]
const-cstr-fork = "0.1"

crate 根目录

#[macro_use] extern crate const_cstr_fork;

示例

 #[macro_use] extern crate const_cstr_fork;
 // Just for the `libc::c_char` type alias.
 extern crate libc;
     
 use std::ffi::CStr;

 const_cstr! {
     HELLO_CSTR = "Hello, world!";

     // Multiple declarations can be made with one invocation.
     // GOODNIGHT_CSTR = "Goodnight, sun!";

     // But only with the same visibility:
     // pub GOODNIGHT_CSTR = "Goodnight, sun!";
     // ^~~ Error: expected identifier, found `pub` 
 }

 // Imagine this is an `extern "C"` function linked from some other lib.
 unsafe fn print_c_string(cstr: *const libc::c_char) {
     println!("{}", CStr::from_ptr(cstr).to_str().unwrap());
 }

 fn main() {
     // When just passed a literal, returns an rvalue instead.
     let goodnight_cstr = const_cstr!("Goodnight, sun!");

     unsafe {
         print_c_string(HELLO_CSTR.as_ptr());
         print_c_string(goodnight_cstr.as_ptr());
     }
 }

打印

Hello, world!
Goodnight, sun!

许可证

许可协议为以下之一:

贡献

除非你明确声明,否则任何有意提交以包含在你所做工作的贡献都将按照上述方式双许可,不附加任何额外条款或条件。

依赖

~43KB