Cstruct requirement of Eio.File.pwrite_single

Hello,
The requirement of a Cstruct when I write is not obvious to me. I didn’t come across any examples in the tutorial. I could only put together this after looking at some issues that used this API. The API doc. doesn’t explain this or anything this has to do with io_uring or something like that ?? Can the doc. itself have pieces of code embedded ?

(e.g) eio 0.11 · OCaml Package which has a small piece of example code.
I

let write () =
 Eio_main.run @@ fun env ->
  let ( / ) = Eio.Path.( / ) in
  let path = Eio.Stdenv.fs env in
  let p = path / "/Users"/"anu"/"Documents"/"rays"/"Bitcask"/"bitcask"/"bitcask.log" in
  let size = if  (Sys.file_exists  "/Users/anu/Documents/rays/Bitcask/bitcask/bitcask.log") then
                     (Eio.Path.stat ~follow:true p).size
                else Optint.Int63.of_int 0 in
  Eio.Path.with_open_out  ~create:(`If_missing 0o600) p
  (fun f ->
      let _ = Eio.File.pwrite_single ~file_offset:size  f [Cstruct.create 128] in
      ()
  )

Thanks

Hi @Mohan_Radhakrishnan,

The Cstruct is necessary to hold the data you wish to write into the file. The are various constructor functions to build Cstructs (e.g. of_string).

In your example you appear to be trying to write to the end of your file. You can do this without having to use the Eio.File API with

let data = Cstruct.of_string "Hello" in
Eio.Path.with_open_out ~append:true ~create:(`If_missing 0o600) p (fun f ->
  Eio.Flow.single_write f [ data ]
)

You could also make use of functions like Eio.Flow.copy_string too to be even more succinct if your data is coming from an OCaml string.

If you’re question is more about the choice of cstructs, there is more information at Decide on cstruct vs bytes · Issue #140 · ocaml-multicore/eio · GitHub

Hope this helps :))

Thanks. That is more compact.