allow deferred close of nil stream (#20706)
* allow deferred close of nil stream * improve example
This commit is contained in:
parent
0a1f204f0f
commit
52166e3546
2 changed files with 20 additions and 4 deletions
|
|
@ -173,10 +173,20 @@ proc close*(s: Stream) =
|
||||||
## See also:
|
## See also:
|
||||||
## * `flush proc <#flush,Stream>`_
|
## * `flush proc <#flush,Stream>`_
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
var strm = newStringStream("The first line\nthe second line\nthe third line")
|
block:
|
||||||
|
let strm = newStringStream("The first line\nthe second line\nthe third line")
|
||||||
## do something...
|
## do something...
|
||||||
strm.close()
|
strm.close()
|
||||||
if not isNil(s.closeImpl): s.closeImpl(s)
|
|
||||||
|
block:
|
||||||
|
let strm = newFileStream("amissingfile.txt")
|
||||||
|
# deferring works even if newFileStream fails
|
||||||
|
defer: strm.close()
|
||||||
|
if not isNil(strm):
|
||||||
|
## do something...
|
||||||
|
|
||||||
|
if not isNil(s) and not isNil(s.closeImpl):
|
||||||
|
s.closeImpl(s)
|
||||||
|
|
||||||
proc atEnd*(s: Stream): bool =
|
proc atEnd*(s: Stream): bool =
|
||||||
## Checks if more data can be read from `s`. Returns ``true`` if all data has
|
## Checks if more data can be read from `s`. Returns ``true`` if all data has
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,12 @@ block tstreams3:
|
||||||
echo line
|
echo line
|
||||||
s.close
|
s.close
|
||||||
|
|
||||||
|
|
||||||
|
block:
|
||||||
|
let fs = newFileStream("amissingfile.txt")
|
||||||
|
defer: fs.close()
|
||||||
|
doAssert isNil(fs)
|
||||||
|
|
||||||
# bug #12410
|
# bug #12410
|
||||||
|
|
||||||
var a = newStringStream "hehohihahuhyh"
|
var a = newStringStream "hehohihahuhyh"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue