V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bli22ard
V2EX  ›  Rust

rust 的 File 是有魔法吗?

  •  
  •   bli22ard · 1 天前 · 517 次点击
    use std::error::Error;
    use std::fs::File;
    use std::io::{BufRead, BufReader};
    
    
    fn main() ->Result<(),Box<dyn Error>> {
        let path=r#"C:\Users\0\Downloads\123.txt"#;
        let fd=File::open(path)?;
        let fd_p=&fd;
        let mut br=BufReader::new(fd_p);
        let mut line=String::new();
        br.read_line(&mut line)?;
        println!("{}",line);
        Ok(())
    }
    

    以上代码,BufReader::new(fd_p); 可以传入 fd_p 也可以传入 fd 都正常工作,感觉读取文件要改变数据才可以读取吧,比如文件指针位置,这个不可变引用 fd_p 读取是怎么做到的

    8 条回复    2024-09-19 17:11:00 +08:00
    Kaleidopink
        1
    Kaleidopink  
       1 天前
    翻一下文档就能发现实现 Read trait 的是 &File, 而不是 File, 所以即便是不可变引用也可以读取. 内部实现则是用了 unsafe.
    bli22ard
        2
    bli22ard  
    OP
       1 天前
    @Kaleidopink File 也有实现 Read trait , 不过实现方式还是用 &File 的实现。 这些其实不是重点,重点是, 为什么读取数据可以是不可变的
    nebkad
        3
    nebkad  
       1 天前
    @bli22ard #2

    https://doc.rust-lang.org/stable/std/fs/struct.File.html

    Note that, although read and write methods require a &mut File, because of the interfaces for Read and Write, the holder of a &File can still modify the file, either through methods that take &File or by retrieving the underlying OS object and modifying the file that way. Additionally, many operating systems allow concurrent modification of files by different processes. Avoid assuming that holding a &File means that the file will not change.
    bli22ard
        4
    bli22ard  
    OP
       1 天前
    @nebkad 搞这个&File 可以读取(引起数据改变)操作比较迷惑,给人的感觉就是不可变突然可以变了
    zizon
        5
    zizon  
       12 小时 4 分钟前
    也容易理解吧.实际 fd 的状态是 os 维护的,rust 顶多是个上层 API 调用方.
    nebkad
        6
    nebkad  
       7 小时 39 分钟前
    @bli22ard 如果你对 std 的设计不是很满意,你的 OS 平台支持你在 Rust 中使用 &mut 语义的话,你自己重新包装一个 File 也不是很难啊
    bli22ard
        7
    bli22ard  
    OP
       6 小时 56 分钟前
    @nebkad 自己实现容易吗? 主要是好奇,把&mut 搞为&,依然可以执行看起来"改变"的操作。感觉 5 楼的解释比较合理。对于 rust 来说,File 就是不可变的,变的那部分在 os 维护,读取的位置,缓冲区等等。
    nebkad
        8
    nebkad  
       6 小时 44 分钟前
    @bli22ard #7

    std 的源码是公开的,你甚至都不用下载直接在我发给你的链接开始点两下就看到了。
    就算你改成了支持 &mut 语义,如果 OS 不支持独占,难道你还能阻止别的调用者用同样的路径再开一个 File 吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2386 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 15:55 · PVG 23:55 · LAX 08:55 · JFK 11:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.