Rust Language Server cannot detect std::os::unix module

Please why can't use

std::os::unix

on Linux Platform. I am currently using ** std::os::unix:**. it is a UNIX like OS

I am trying to use RawFd and the like

What's the exact code you wrote, and what error messages does the compiler display?

Here is the code, I am learning the book Unix Network Programming and also learning the FFI interface without using Bingen. I needed to convert Unix FD to Rust a rust reader and writer (File), so I need the FromRawFd and Raw but notice auto-complete was not working, at first I thought it was my vim configuration and open up Clion same thing. but the code compiles fine and even provided Error suggestion on missing argument.

#![allow(dead_code)]
use libc::{in_addr, sockaddr_in, sockaddr, AF_INET, SOCK_STREAM};
use std::io;
use std::os::unix::io::{RawFd, FromRawFd};

use std::fs::File;

const MAXLINE: usize = 100;

fn main() {
    // variable
    let sockfd: RawFd;
    let recvline: [u8; MAXLINE];
    let ip_address: sockaddr_in;

    // read command line argument
    // TODO: read IP address from the command line

    let ip_address  = u32::from_ne_bytes([127, 0, 0, 1]);
    let sockaddr_ = sockaddr_in {
        sin_family: AF_INET as u16,
        sin_port: 9999u16.to_be(),
        sin_addr: in_addr {s_addr: ip_address},
        sin_zero: [0u8; 8]
    };

    // create socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0).unwrap();

    // connect to daytime server
    match connect(sockfd, &sockaddr_) {
        Err(err) => std::process::exit(1),
        _ => {},
    }

    // conver linux fd to Rust Reader and Writer
    let file_decr = unsafe{ File::from_raw_fd(sockfd)};


}

// create a  socket and return a  file descriptor
fn socket(domain: i32, type_: i32, protocol: i32) -> Result<i32, io::Error> {
    unsafe {
        match libc::socket(domain, type_, protocol) {
            err if err < 0 => Err(io::Error::last_os_error()),
            fd => Ok(fd)
        }
    }
}

// bind to a port
fn bind(sockfd: i32, sockaddr: *const libc::sockaddr) -> io::Result<()> {
    let sockaddr_len = ::std::mem::size_of::<sockaddr>() as u32;

    let ret = unsafe {
        libc::bind(sockfd, sockaddr, sockaddr_len)
    };

    if ret < 0 { Err(io::Error::last_os_error()) } else { Ok(()) }
}

// connect to a server
fn connect(sockfd: i32, sockaddr: &sockaddr_in) -> io::Result<()> {
    let sockaddr_len = std::mem::size_of::<sockaddr>() as u32;
    let ret = unsafe {
        libc::connect(sockfd, sockaddr as *const sockaddr_in as *const sockaddr, sockaddr_len)
    };

    match ret {
        val if val < 0 => Err(io::Error::last_os_error()),
        _ => Ok(())
    }
}


some pictures, though not very clear

Clion autocomplete also not seeing Unix only raw that is available on Linux


Vim LSP showing unresolved import


But still showed missing argument error

from_raw_fd() needs to receive the fd that you want to use as a file. It can't make a file out of a thin air.

1 Like

Is there any reason that your IDEs would be set up to compile for a different platform? Are you using the Windows Subsystem for Linux (WSL), for example?

I use Linux OpenSuse Tumbleweed. The thing is, just like to pictures, it is not just affecting Clion, also affecting Vim, I am using the Rust Language Server. Because it is affecting two non-related IDE/Text Editors I am thinking maybe it is the RLS that might be causing this.

This third picture was intentional, I was expecting something in the line of "Unresolved import" instead the compiler actually detected that the function is missing an argument.

But why is the Language server not detecting the Unix and other related modules but the compiler can?

This third picture was intentional, I was expecting something in the line of "Unresolved import" instead the compiler actually detected that the function is missing an argument.

seems compiler sees the module but RLS can't

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.