ckb_std/
env.rs

1//! Inspection and manipulation of the program’s environment.
2
3use core::{
4    ffi::{CStr, c_char},
5    fmt::Debug,
6    ops::Deref,
7};
8
9/// An argument passed to this program.
10#[repr(transparent)]
11pub struct Arg(*const c_char);
12
13impl Debug for Arg {
14    #[inline]
15    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16        self.deref().fmt(f)
17    }
18}
19
20impl From<&'static CStr> for Arg {
21    #[inline]
22    fn from(arg: &'static CStr) -> Self {
23        Self(arg.as_ptr())
24    }
25}
26
27impl Deref for Arg {
28    type Target = CStr;
29    #[inline]
30    fn deref(&self) -> &Self::Target {
31        unsafe { CStr::from_ptr(self.0) }
32    }
33}
34
35#[cfg(feature = "native-simulator")]
36impl Arg {
37    pub fn new(arg: &str) -> Self {
38        Self {
39            0: (arg.as_ptr()) as *const c_char,
40        }
41    }
42}
43
44static mut ARGV: &'static [Arg] = &[];
45
46/// Returns the arguments that this program was started with (normally passed
47/// via `exec` or ckb-debugger).
48///
49/// (Not to be confused with **script args** when used as cell lock script or
50/// type script. That has be loaded with `load_script`.)
51#[inline]
52pub fn argv() -> &'static [Arg] {
53    unsafe { ARGV }
54}
55
56// For native-simulator and entry!.
57#[doc(hidden)]
58#[inline]
59pub unsafe fn set_argv(argv: &'static [Arg]) {
60    unsafe { ARGV = argv }
61}