Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 你能把mut i8转换成i32吗?_Unix_Casting_Rust_Pty - Fatal编程技术网

Unix 你能把mut i8转换成i32吗?

Unix 你能把mut i8转换成i32吗?,unix,casting,rust,pty,Unix,Casting,Rust,Pty,我试图构建一个小型终端仿真器,但遇到了一些与libc有趣的类型冲突。当我试图设置pty连接的从属部分时,我需要通过对ptsname()的系统调用来创建从属部分,以便获取pts的名称,以便访问它。但是,我得到一个类型错误,表示libc::ptsname()需要输入一个i32。这与手册页直接冲突,手册页说它应该被传递一个文件描述符。我只是想知道我是否可以将文件描述符的libc::c_int转换为i32以传递到ptsname 代码如下: use libc::{self, c_int, grantpt,

我试图构建一个小型终端仿真器,但遇到了一些与libc有趣的类型冲突。当我试图设置pty连接的从属部分时,我需要通过对ptsname()的系统调用来创建从属部分,以便获取pts的名称,以便访问它。但是,我得到一个类型错误,表示libc::ptsname()需要输入一个i32。这与手册页直接冲突,手册页说它应该被传递一个文件描述符。我只是想知道我是否可以将文件描述符的libc::c_int转换为i32以传递到ptsname

代码如下:

use libc::{self, c_int, grantpt, posix_openpt, ptsname, unlockpt, O_RDWR};
use std::os::unix::io::FromRawFd;
use std::process::{Child, Command, Stdio};

#[derive(Debug)]
pub struct Pty {
    process: Child,
    fd: i32,
}

fn create_pty(process: &str) -> Pty {
    let master: c_int;
    unsafe {
        // create master/slave pair of fd
        master = posix_openpt(O_RDWR);
        if master == -1 {
            panic!("Failed to posix_openpt");
        }

        // set slave ownership and mode as master
        let mut result = grantpt(master);
        if result == -1 {
            panic!("Failed to grantpt");
        }
        // unlock slave
        result = unlockpt(master);
        if result == -1 {
            panic!("Failed to unlockpt");
        }
    }
    let slave: c_int = ptsname(master as i32);
    slave = libc::open(slave);

    let mut builder = Command::new(process);

    match builder.spawn() {
        Ok(process) => {
            let pty = Pty {
                process,
                fd: master,
            };

            pty
        }
        Err(e) => {
            panic!("Failed to create pty: {}", e);
        }
    }
}

fn main() {
    let shell = "/bin/bish";

    let pty = create_pty(shell);
    println!("{:?}", pty);
}

和控制台输出(第二个错误现在可以忽略):


这并不是说它需要输入
i32
,而是说您要求的是
ptsname(master as i32)
的类型为
i32
。这可能有点让人困惑,因为
c_int
i32
的别名,所以听起来像是在要求一个不相关的类型


问题是,当
ptsname
返回
*mut c_char
c_char
也是一个别名,这一次用于
i8
)时,您正在给
slave
类型
c_int

感谢您的回复。我在ptsname的返回类型中出错。
error[E0308]: mismatched types
  --> src/main.rs:42:24
   |
42 |     let slave: c_int = ptsname(master as i32);
   |                        ^^^^^^^^^^^^^^^^^^^^^^ expected i32, found *-ptr
   |
   = note: expected type `i32`
              found type `*mut i8`

error[E0060]: this function takes at least 2 parameters but 1 parameter was supplied
  --> src/main.rs:43:13
   |
43 |     slave = libc::open(slave);
   |             ^^^^^^^^^^^^^^^^^ expected at least 2 parameters

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0060, E0308.
For more information about an error, try `rustc --explain E0060`.
error: could not compile `experiment`.