-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
more detailed docstring for read! and write #49769
base: master
Are you sure you want to change the base?
Conversation
base/io.jl
Outdated
read!(stream::IO, a::Ref) | ||
read!(filename::AbstractString, a::Union{AbstractArray, Ref}) | ||
|
||
Read `sizeof(a)` bytes of binary data from an I/O stream or file, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite right — the number of bytes is only sizeof(a)
if isbitstype(eltype(a))
.
A more general and accurate description would be:
Read `length(a)` consecutive values of type `eltype(a)` from an I/O stream or file into `a`, returning `a`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the function even work for non-isbits?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Seelengrab: If isbitstype(T)==false
, then read!(..., a::AbstractArray{T,N})
just runs
for i in eachindex(a)
a[i] = read(s, T)
end
i.e. falls back to whatever read()
method was provided for type T
, and the number of bytes read will depend on what that does. Also, it seems read!(..., a::Ref{T})
currently doesn't have such a fallback and therefore is only intended for isbitstype(T)==true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's what I mean - non-bitstypes can't really be read like that, because of the pointers contained within. They need to be properly deserialized and recreated in a more complicated manner.
The old `read!` docstring failed to describe how many bytes the function reads and what its return value is. The old `write` docstring didn't cover arrays. I also documented `read!(..., Ref{T})`: I first wasn't sure if this was meant to be documented, but then saw that it was already mentioned in the docstring of `write`.
I've now updated this docstring PR:
|
User-defined plain-data types without `write` methods can be written when wrapped in a `Ref`: | ||
Arrays: | ||
```jldoctest | ||
julia> write(stdout, [0x41 0x42; 0x43 0x44], 0x0a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
julia> write(stdout, [0x41 0x42; 0x43 0x44], 0x0a) | |
julia> write(stdout, UInt8['A' 'B'; 'C' 'D'], '\n') |
If `a` is an `Array` and `isbitstype(T)` is true, this function reads | ||
`size(a)` bytes directly into memory. Otherwise, each element of | ||
`a::AbstractArray{T}` is read with a call to `read(stream, T)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If `a` is an `Array` and `isbitstype(T)` is true, this function reads | |
`size(a)` bytes directly into memory. Otherwise, each element of | |
`a::AbstractArray{T}` is read with a call to `read(stream, T)`. | |
If `isbitstype(T)` is true, this function reads `sizeof(a)` bytes | |
directly into memory. Otherwise, each element of `a` is populated | |
sequentially by a call to `read(stream, T)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if sizeof(a)
is accurate here.
Base.sizeof
is not defined for most AbstractArray
and it falls back to Core.sizeof
by default.
BTW, if we expect AbstractArray
s to behave like Array
s, then the optimization for StridedArray
s needs to check elsize(A) == elsize(Array{eltype(A)})
to avoid possible padding mismatch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, maybe sizeof(T) * length(a)
bytes would be better then? (more accurate yet would be Base.aligned_sizeof
, but that seems a bit overly pedantic to me)
The old
read!
docstring failed to describe how many bytes thefunction reads and what its return value is.