Downsides of C language culture?
Downsides of C language culture?
Posted Aug 23, 2024 17:30 UTC (Fri) by NYKevin (subscriber, #129325)In reply to: Downsides of C language culture? by viro
Parent article: A review of file descriptor memory safety in the kernel
This is spelled std::borrow::Cow. Wrap it in Option<T> if you want None as well (which I believe should have no size overhead, because one of the Cow enum variants has a reference in it, and references are non-nullable, so there's a niche the compiler can use for Option layout optimization).
But Cow has its own problems, because it is specifically designed for borrow-checked references. If you want something less restrictive, like refcounted references, then you probably need to use Rc/Arc::make_mut() instead (or one of its sister methods like unwrap_or_clone()).
> Or do you mean that all abstractions must be there from the very beginning? I'm not fond of Rust as a language, but I think you are doing it a disservice here...
In this case, I think the argument is that Rust enforces struct field visibility, so you can't just reach into a Rust struct and fiddle with its members to represent arbitrary states. You would be forced to wrap it in some other struct or enum that describes your additional state data. In practice, you would probably just use Result<T, E> for this use case, because then you get the question mark operator etc. for free.
Posted Aug 23, 2024 19:53 UTC (Fri)
by viro (subscriber, #7872)
[Link] (1 responses)
Posted Aug 27, 2024 4:45 UTC (Tue)
by NYKevin (subscriber, #129325)
[Link]
Of course, C does not even have visibility restrictions in the first place, except for really blunt measures like an opaque struct. In Rust, visibility restrictions are considered part of the safety boundary - there are plenty of APIs in the standard library and third-party crates which rely on visibility restrictions to maintain safety. For example, if you reach into a Vec and change its size or capacity, you can cause it to read uninitialized memory or perform other kinds of UB. But that's still considered a sound API, because you can't reach into a Vec and fiddle with its private members (without writing unsafe code that does some kind of type-punning).
Anyway, the end result of all this is that, in Rust, if you care to do so, it is possible to design a struct or enum in such a way that it can only represent those states you actually want it to represent, and anyone who manages to get it to represent some other state has already done UB before your code even runs.
Posted Aug 26, 2024 11:37 UTC (Mon)
by taladar (subscriber, #68407)
[Link]
Downsides of C language culture?
Downsides of C language culture?
Downsides of C language culture?