-
-
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
RFC: ByteOrder types for io #3633
Conversation
Cc: @loladiro |
There needs to be a generic fallback that defaults to |
read(s::IO, ::Type{Bool}) = (read(s,Uint8)!=0) | ||
read(s::IO, ::Type{Float32}) = box(Float32,unbox(Int32,read(s,Int32))) | ||
read(s::IO, ::Type{Float64}) = box(Float64,unbox(Int64,read(s,Int64))) | ||
read{B<:ByteOrder}(s::IO, ::Type{Float32}, ::Type{B}) = box(Float32,unbox(Int32,read(s,Int32,B))) |
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.
while you're at it, this is much more nicely done using reinterpret
instead of box/unbox.
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.
I don't know... reinterpret
for single values make a lot more of operations.
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.
that's not 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.
julia> function bar(x)
Intrinsics.box(Float64,Intrinsics.unbox(Int64,x))
end
# methods for generic function bar
bar(x) at none:2
julia> disassemble(bar,(Int64,))
define double @julia_bar621(i64) {
top:
%1 = bitcast i64 %0 to double, !dbg !5031
ret double %1, !dbg !5031
}
julia> function foo(x)
reinterpret(Float64,x)
end
# methods for generic function foo
foo() at none:2
foo(x) at none:2
julia> disassemble(foo,(Int64,))
define double @julia_foo600(i64) {
top:
%1 = bitcast i64 %0 to double, !dbg !4869
ret double %1, !dbg !4869
}
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.
Good :) I belief that reinterpret
makes conversion from value to array and get a value again by definition on Array. Based on function declaration at array.jl
Maybe it's something more to add into the style notes of the docs ;)
There is a pure-bits read/write method for arrays which will need to be adjusted. |
I've added As for the fallback: there's one for |
One other point: should |
This looks great. I'd say if you can resolve the merge conflict and perhaps write a few more tests (e.g., floating-point scalars, etc.), then we should merge this. |
I suppose if this is merged StrPack should switch to this type. (Previously, it had been changed from a type to symbols due to the language zeitgeist...is the pendulum swinging back?) Of course would also take advantage of the updated method signatures. |
Okay, I've rebased to master, and added some more tests. I'm still not sure about the interface
I haven't really kept up with the current trends re: tags. The only other place in base where they seem to be widely used is sort.jl, but they do it slightly differently by exporting |
I think for low-level IO keyword args are not the right way to go. This leverages dispatch. I think it's plenty good, and the issue about const instances can be addressed later if someone thinks it's important. Enough bikeshedding. |
I guess I'm a little late here, but I object to this interface. Primarily my objection is that byte order should be property of streams, not data, and certainly shouldn't normally be something that is specified on individual reads and writes. I'm also not super thrilled about the API of passing these network byte order objects as straggling arguments. |
Well, what about if we use a type wrapper around IO types? Something like:
|
That seems much better to me. I think @JeffBezanson has some opinions here as well. |
Stdlib: Pkg URL: https://github.com/JuliaLang/Pkg.jl.git Stdlib branch: master Julia branch: master Old commit: b02fb9597 New commit: ffb6edf03 Julia version: 1.11.0-DEV Pkg version: 1.11.0 Bump invoked by: @IanButterworth Powered by: [BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl) Diff: JuliaLang/Pkg.jl@b02fb95...ffb6edf ``` $ git log --oneline b02fb9597..ffb6edf03 ffb6edf03 cache pidlock tweaks (#3654) 550eadd7e Pin registry for MetaGraph tests (#3666) ee39026b8 Remove test that depends on Random being in the sysimg (#3656) 561508db2 CI: Increase the CI timeout. Update actions. Fix double precompilation. (#3665) 7c7ed63b1 Remove change UUID script it should be uncessary on Julia v1.11-dev (#3655) a8648f7c8 Precompile: Fix algorithmic complexity of cycle detection (#3651) 0e0cf4514 Switch datastructure Vector -> Set for algorithmic complexity (#3652) 894cc3f78 respect if load-time precompile is disabled (#3648) 3ffd1cf73 Make auto GC message use printpkgstyle (#3633) ``` Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
This adds
ByteOrder
types and read/write methods to make it easier to deal with different byte orderings.The key advantage is arrays: previously I found myself using code such as:
Now it is possible to use
It still needs some tests, documentation and exports, but in the meantimeI would appreciate any feedback on the interface.