How can I create an in_channel from a string?

The small-string limitation is not a problem for me because I know the string length and it is small, but I do need to mention it in the documentation. And thank you for pointing out the bug: I need to close out_channel, not just flush it.

let in_channel_of_string string =
  let (in_file_descr, out_file_descr) = Unix.pipe () in
  let in_channel = Unix.in_channel_of_descr in_file_descr in
  let out_channel = Unix.out_channel_of_descr out_file_descr in
  begin
    output_string out_channel string;
    close_out out_channel;
    in_channel;
  end
1 Like