Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / file-desc.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8structure FileDesc: FILE_DESC =
9 struct
10 open Posix.IO Posix.FileSys
11
12 type t = file_desc
13
14 val toString = SysWord.fmt StringCvt.DEC o fdToWord
15 val layout = Layout.str o toString
16
17 fun move {from, to} =
18 if from <> to
19 then (dup2 {old = from, new = to}
20 ; close from)
21 else ()
22
23 fun fluidLet (d1, d2, f) =
24 let
25 val copy = dup d1
26 val _ = dup2 {old = d2, new = d1}
27 in
28 Exn.finally (f, fn () => move {from = copy, to = d1})
29 end
30 end