3 ms·
For example of the problem that the API would have if it wanted to handle OOM: Box, String or Vec, the most basic types in the rust alloc crate, couldn't implem
by varajelle 6y ago
For example of the problem that the API would have if it wanted to handle OOM: Box, String or Vec, the most basic types in the rust alloc crate, couldn't implement Clone, because clone() can't fail, and you need to allocate memory to clone these data structures.
As a result, lots of generic algorithms that relies on on their type to be clonable wouldn't work, and most user types could not #[derive(Clone)] anymore.
- volta83 6y agoHows that' a problem? Provide `TryClone`, there, problem solved.
- loeg 6y agoI agree, it doesn't seem like these types can actually implement Clone if allocation can fail.
- astrobe_ 6y agoSo, if I get this right, this means that the "just don't use the alloc crate" argument given somewhere else here is not practical ?
- pixel_fcker 6y agoNo not at all.
- astrobe_ 6y agoBecause ...? I mean, from the perspective of an outsider reading that you "can't use strings" if there's a risk of allocation failure is a bit worrying.
- pixel_fcker 6y agoBecause you just don’t use the types in the alloc crate, or you use the (only partially implemented) APIs that return a Result<> instead of panicking on allocation failure. For that kind of low-level work I’d probably do the former and implement my own data structures, which is what you do with no_std currently. As others have said there’s nothing in Rust the language which assumes allocations always succeed. The standard library made the choice to panic on allocation failure, which was the right choice in order to make an ergonomic API that’s suitable for higher-level work. If you’re not using the standard library (which currently includes people on eg embedded targets) then you can handle allocation any way you like. And if you’re writing drivers for the kernel you’d assume you’re ok handling that. GP was pointing out that the Clone trait doesn’t allow for returning a Result, so you can’t use that trait to clone anything that allocates. But that’s not really a big deal since you can just make your own TryClone that does.
- astrobe_ 6y agoOk, thanks for the clarification.
- varajelle 6y agoNo. People that care about catching OOM failures will simply have to use other crates that have a more complex API, but that's what they want I was just saying it is a right choice in the alloc crate, which is meant for user space programs, to simply panic on case of OOM, because otherwise the API would be more complex for everyone.