by nequo on 8/30/24, 4:15 AM with 165 comments
by sedatk on 8/30/24, 6:38 AM
by wiz21c on 8/30/24, 7:20 AM
Interestingly, my life starts at the end of the article, with the simple verison of the code, and as my understanding of rust widens, I go up to the beginning of the article and better define my function...
by tmtvl on 8/30/24, 11:33 AM
(defun read (path)
(declare (generic P (AsRef Path))
(type P path)
(returns (io:Result (Vector U8))))
(flet ((inner (path)
(declare (type (Ref Path) p)
(returns (io:Result (Vector U8))))
(try-let ((file (File:open path))
(bytes (vector)))
(declare (mutable file bytes))
(try (read-to-end file bytes)
(Ok bytes)))))
(inner (as-ref path))))
by MetricExpansion on 8/30/24, 7:20 AM
@usableFromInline
func _read(pathView: PathView) throws(IOError) -> [UInt8] {
var file = try File(pathView)
var bytes: [UInt8] = []
try file.readToEnd(into: &bytes)
return bytes
}
@inlinable
public func read<Path>(path: borrowing Path) throws(IOError) -> [UInt8] where Path: PathViewable, Path: ~Copyable {
try _read(pathView: path.view())
}
// Definitions...
public enum IOError: Error {}
public protocol PathViewable: ~Copyable {
func view() -> PathView
}
public struct PathView: ~Escapable {}
public struct File: ~Copyable {
public init(_ pathView: borrowing PathView) throws(IOError) {
fatalError("unimplemented")
}
public mutating func readToEnd(into buffer: inout [UInt8]) throws(IOError) {
fatalError("unimplemented")
}
}
by jiwangcdi on 8/30/24, 8:05 AM
I can't understand this. Isn't this for polymorphism like what we do this:
```rust fn some_function(a: impl ToString) -> String { a.to_string(); } ```
What to do with memory layout? Thanks for any explanation.
by eterps on 8/30/24, 6:44 AM
by anonymous2024 on 8/30/24, 8:45 AM
by librasteve on 8/30/24, 3:51 PM
pub fn read(path: Path) -> Bytes {
let file = File::open(path);
let bytes = Bytes::new();
file.read_to_end(bytes);
bytes
}
Here is is in raku (https://raku.org): sub read(Str:D $path --> Buf:D) {
$path.IO.slurp: :bin
}
[the `--> Buf:D` is the raku alternative to monads]by qalmakka on 8/30/24, 10:26 AM
by tevelee on 8/30/24, 1:06 PM
by apatheticonion on 8/30/24, 10:19 AM
by mgaunard on 8/30/24, 8:56 AM
by AxelLuktarGott on 8/30/24, 6:39 AM
by Woshiwuja on 8/30/24, 10:20 AM
by macmac on 8/30/24, 7:21 AM
by mjburgess on 8/30/24, 7:00 AM
When type signatures are so complex it makes vastly more sense to separate them out,
Consider,
read :: AsRef(Path) -> IO.Result(Vec(U8))
pub fn read(path):
inner :: &Path -> IO.Result(Vec(U8))
fn inner(path):
bytes := Vec.new()
return? file := File.open(path)
return? file.read_to_end(&! bytes)
return OK(bytes)
inner(path.as_ref())
by remcob on 8/30/24, 7:00 AM
pub fn read(path: Path) -> Bytes {
File::open(path).read_to_end()
}
by singularity2001 on 8/30/24, 1:38 PM
"I think that most of the time when people think they have an issue with Rust’s syntax, they actually object to Rust’s semantics."
You think wrong. Rust syntax is horrible because it is verbose and full of sigilsby oguz-ismail on 8/30/24, 6:45 AM