File::create("hello.txt")

In the below code, where is the target of Write information encoded?
thanks

fn say_hello(out: &mut dyn Write) -> std::io::Result<()> {
out.write_all(b"hello world\n")?;
out.flush();
}

...
let mut local_file = File::create("hello.txt")?;
say_hello(&mut local_file)?;

It's not clear what you are asking. What do you mean by "target" and "encoded"?

2 Likes

If you mean the directoy in which the hello.txt file is created, it will be the current working directory.

1 Like

Maybe OP is asking about trait objects and how they work? In which case I find the reference a decent read: Trait object types - The Rust Reference. To quote from it:

Each instance of a pointer to a trait object includes:

  • a pointer to an instance of a type T that implements SomeTrait
  • a virtual method table, often just called a vtable, which contains, for each method of SomeTrait and its supertraits that T implements, a pointer to T's implementation (i.e. a function pointer).

So when you pass &mut dyn Write you have a pointer to File and a vtable that stores function pointers to the implementation of the Write methods of File. As to how File is implemented, that depends on your OS. On Unix, File would be some file descriptor handled by your operating system (I guess).

1 Like

In other languages, file create() may return fd(file descriptior) or fp(file pointer) for write mode.
But here in Rust code, local_file is of type Result, so where the destination(-- i.e. fd or fp) is encoded,
unless (fd or fp) is a hidden field of Result?

Does it make sense?
thanks
sun

It is not, because you used the ? operator on it to propagate any errors. local_file has type File.

Again, how the Writer knows whare to write? I don't see how the Writer knows where/target to write.

thanks
sun

The File type is just a wrapper for whatever the platform's abstraction is for files. It too contains a file descriptor or something similar, ultimately.

1 Like

so Writer trait somehow encode fd/fp?

thanks
sun

Read the reply by @jofas.

1 Like

No, not the Write trait itself. It is the File type that contains a file descriptor or equivalent, somewhere deep down below various layers of platform abstractions.

There's no magic, really. In C, how does FILE * "know" where to write? The exact same applies to Rust's File type.

1 Like

Maybe it's worth to note that &mut local_file gets coerced to a wide pointer (where the meta-data of the wide pointer points to the vtable), is that right?. Thus the called say_hello method knows how to write in this case (and that includes how to use the File's internal file descriptor).

(But I'm on mobile phone right now, so can't explain the full chain.)


So my point is: For say_hello being able to know how and where to write, both components of the wide-pointer are needed in the given example.

2 Likes

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.