最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - How to read OS specific parts in the standard library? - Stack Overflow

programmeradmin1浏览0评论

I was just learning about how files are handled in Linux and I started wondering how the std::fs::File abstraction was created in Rust. However I could not find the relevant code when navigating the code.

std::fs::File has a struct defined like this:

pub struct File {
    inner: fs_imp::File,
}

So I guessed that I needed to find what fs_imp::File is. Turns out this fs_imp comes from here which is defined at the top of std::fs::File:

use crate::sys::fs as fs_imp;

Following crate::sys lead me to /.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/mod.rs

At this point I am stumped because this is just a file that looks like this:

#![allow(unsafe_op_in_unsafe_fn)]

/// The PAL (platform abstraction layer) contains platform-specific abstractions
/// for implementing the features in the other submodules, e.g. UNIX file
/// descriptors.
mod pal;

mod personality;

pub mod backtrace;
pub mod cmath;
pub mod exit_guard;
pub mod os_str;
pub mod path;
pub mod sync;
pub mod thread_local;

// FIXME(117276): remove this, move feature implementations into individual
//                submodules.
pub use pal::*;

So how can I understand how the Linux version of Rust files work under the hood?

I was just learning about how files are handled in Linux and I started wondering how the std::fs::File abstraction was created in Rust. However I could not find the relevant code when navigating the code.

std::fs::File has a struct defined like this:

pub struct File {
    inner: fs_imp::File,
}

So I guessed that I needed to find what fs_imp::File is. Turns out this fs_imp comes from here which is defined at the top of std::fs::File:

use crate::sys::fs as fs_imp;

Following crate::sys lead me to /.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/mod.rs

At this point I am stumped because this is just a file that looks like this:

#![allow(unsafe_op_in_unsafe_fn)]

/// The PAL (platform abstraction layer) contains platform-specific abstractions
/// for implementing the features in the other submodules, e.g. UNIX file
/// descriptors.
mod pal;

mod personality;

pub mod backtrace;
pub mod cmath;
pub mod exit_guard;
pub mod os_str;
pub mod path;
pub mod sync;
pub mod thread_local;

// FIXME(117276): remove this, move feature implementations into individual
//                submodules.
pub use pal::*;

So how can I understand how the Linux version of Rust files work under the hood?

Share Improve this question edited Feb 7 at 18:09 Charles Duffy 296k43 gold badges431 silver badges484 bronze badges asked Feb 6 at 8:20 DorukDoruk 2231 silver badge5 bronze badges 1
  • 1 The tag std is for C++ programming language. – 3CxEZiVlQ Commented Feb 7 at 15:16
Add a comment  | 

1 Answer 1

Reset to default 4

You just keep following the imports. The only thing that could bring fs into scope here is the wildcard reexport pub use pal::*; which leads you to sys/pal/mod.rs where depending on the platform another module is wildcard reexported:

cfg_if::cfg_if! {
    if #[cfg(unix)] {
        mod unix;
        pub use self::unix::*;
    }
// …

and if we follow for example the unix path to sys/pal/unix/mod.rs we finally found the definition of the fs module and it contains the definition of File:

pub struct File(FileDesc);

Of course if you have for example rust-analyzer or something similar setup you can just "goto definition" until you arrive at your target which makes all that manual search a lot easier.

发布评论

评论列表(0)

  1. 暂无评论