sourceModuleAtomicIO.tri (986B)
1 !import "io" !Local 2 !import "prelude" !Local 3 path = "__PATH__" 4 check condition = pure (if condition (ok t t) (err "atomic IO mismatch" t)) 5 main = io (onResult_ (do onOk_ 6 first <- writeBytesIfAbsent path [(0) (255)] 7 _ <- check first 8 second <- writeBytesIfAbsent path [(1)] 9 _ <- check (not? second) 10 before <- readFile path 11 _ <- check (equal? before [(0) (255)]) 12 _ <- atomicWriteBytes path [(7)] 13 after <- readFile path 14 _ <- check (equal? after [(7)]) 15 _ <- onResult_ (sha256Hex [(256)]) 16 (problem : pure (if (equal? problem "invalid bytes") 17 (ok t t) (err "wrong invalid-bytes error" t))) 18 (_ : pure (err "accepted invalid byte" t)) 19 _ <- onResult_ (readFile (append path ".missing")) 20 (problem : pure (if (equal? problem "does not exist") 21 (ok t t) (err "wrong missing-file error" t))) 22 (_ : pure (err "accepted missing file" t)) 23 pure (ok t t)) 24 (problem : putStrLn (append "atomic-io-failed: " problem)) 25 (_ : putStrLn "atomic-io-ok")) 26 main