News for hackers. Stuff that matters.
Re:Tracking Trust with Rust in the Kernel by lock1 on Friday September 19, 2025 @12:37AM (#45297999)
Why is that not feasible? You could define `Untrusted<T>` container and `.map()` in Java just fine.
Re:Tracking Trust with Rust in the Kernel by lmm on Friday September 19, 2025 @03:58AM (#45299084)
Java lacks higher-kinded types, so there is no library of helpful functions that work on anything with a .map() method. E.g. if you want to do a tree traversal you'll have to implement it by hand.
Re:Tracking Trust with Rust in the Kernel by taktoa on Friday September 19, 2025 @03:33AM (#45298919)
Rust and C++ implement generics with monomorphization rather than boxing, so there is a potential performance hit associated with a type like this in Java that is guaranteed not to exist in Rust.

In practice, the JVM may still monomorphize it, but it is not guaranteed to, and this would be a good reason to avoid unnecessary uses of generics in a high performance codebase like a kernel, if you chose to write one in Java.

Re:Tracking Trust with Rust in the Kernel by lock1 on Friday September 19, 2025 @05:04AM (#45299517)
Sure, I guess that's worth mentioning in the context of the original post. It's true that Java implementation of parametric polymorphism has a performance drawback compared to Rust or C++. And it's certainly a bad idea to use Java generics without considering the drawback in hot code paths.

But GP described something they wanted from a type system and basically said container with `Functor`-like behavior is not possible to do in Java. It's possible, albeit with a performance drawback and a bit more clunky to work with compared to Rust, Haskell, or a language with native HKT support.

Re:Tracking Trust with Rust in the Kernel by wavemode on Friday September 19, 2025 @08:35AM (#45300944)
the parent commenter states that Java is their everyday language, so in this context I don't think we're talking about performance, nor about the needs of the kernel.
Re:Tracking Trust with Rust in the Kernel by menaerus on Friday September 19, 2025 @02:03AM (#45298405)
Yes, it can be implemented basically in any language that can hide the data members so I also see nothing special about it.
Re:Tracking Trust with Rust in the Kernel by surajrmal on Friday September 19, 2025 @02:15AM (#45298467)
It's only special when you are coming from C. Kernels implemented in c++ have done this sort of thing for a long time.
Re:Tracking Trust with Rust in the Kernel by kimixa on Friday September 19, 2025 @03:12AM (#45298780)
There's plenty of C string libraries with opaque types. No reason why a similar thing can't be done there.
Re:Tracking Trust with Rust in the Kernel by menaerus on Friday September 19, 2025 @04:53AM (#45299449)
Opaque types cannot be stack- or statically allocated.
Re:Tracking Trust with Rust in the Kernel by kimixa on Friday September 19, 2025 @05:04AM (#45299522)
No, but many languages already have limitations like "strings live on the heap", even many discussed in this thread.

Dynamically sized objects on the stack has always been difficult. You could argue that C special casing fixed length strings to allow that is the odd one out.

Re:Tracking Trust with Rust in the Kernel by ViewTrick1002 on Friday September 19, 2025 @02:56AM (#45298680)
Not really. Hiding the data is the easy part.

What makes Rust special is that you need to acknowledge the potential errors when unwrapping the type. With a standard library and culture built on exposing the edge cases at compile time.

That is where the guarantees and feeling of certainty comes from.

Re:Tracking Trust with Rust in the Kernel by lock1 on Friday September 19, 2025 @03:20AM (#45298837)
Visibility modifier as a native language feature is not the easy part.

It's more like "comptime safety feeling" => "language w/ visibility modifier" but the converse is not necessarily true. Without language support, it's back to C convention or workaround again.

Re:Tracking Trust with Rust in the Kernel by menaerus on Friday September 19, 2025 @04:44AM (#45299394)
Show me an example. I don't understand this marketing lingo.
Re:Tracking Trust with Rust in the Kernel by ViewTrick1002 on Friday September 19, 2025 @05:07AM (#45299540)
Take for example working with file names and paths. In for example Linux paths are a collection and bytes and does not need to be valide unicode. Which some programming languages hides from you and subtly introduces errors. Or even worse, accepting that strings may contain invalid unicode poisoning the entire language with uncertainty.

If you are iterating over the files in a directory the Rust standard library gives you paths. Not strings.

To convert a path that to a regular string type which is valid unicode you need to acknowledge that the path may be invalid. Either unwrapping and panicking or handling the error.

To do this the standard library gives two options:

  /// Yields a [`&str`] slice if the `Path` is valid unicode.
  ///
  /// This conversion may entail doing a check for UTF-8 validity.
  /// Note that validation is performed because non-UTF-8 strings are
  /// perfectly valid for some OS.
  
  pub fn to_str(&self) -> Option<&str>
https://doc.rust-lang.org/std/path/struct.PathBuf.html#metho...

Or accept that the conversion may be lossy:

  /// Converts a Path to a Cow<str>.
  ///
  /// Any non-UTF-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER.
  
  pub fn to_string_lossy(&self) -> Cow<'_, str>
https://doc.rust-lang.org/std/path/struct.PathBuf.html#metho...

You get to make a choice, and panicking/erroring is perfectly valid if you only expect valid unicode paths.

As the world and your software changes if you suddenly encounter a non-unicode path then you will immediately know where the error comes from and can fix the issue. Instead of trying to pinpoint the root source of an error far exposing itself far down stream.

Re:Tracking Trust with Rust in the Kernel by menaerus on Friday September 19, 2025 @05:16AM (#45299599)
You said

> With a standard library and culture built on exposing the edge cases at compile time.

So, how exactly is "you need to acknowledge that the path may be invalid" from your example a compile-time trait? That's certainly not something you know during the compile-time, and if so, then you should be treating all the files like that, so, I see no difference here wrt other languages.

Re:Tracking Trust with Rust in the Kernel by ViewTrick1002 on Friday September 19, 2025 @05:25AM (#45299654)
Take the function signature:

  pub fn to_str(&self) -> Option<&str>
An option is an enum (tagged union)

  pub enum Option<T> {
      None,
      Some(T),
  }
To get the data out of the Some variant you need to match on it. If you don't cover the the None variant with a choice it will not compile.

  let my_valid_string = match path.to_str() {
      Some(path) => path,
      None => todo!("Cover invalid unicode case"),
  }
You can see this in the playground here were I deliberately did not cover the None path leading to the compiler telling me I need to make a choice.

https://play.rust-lang.org/?version=stable&mode=debug&editio...

We can't know if a generic path is valid at compile time. But we can at compile time ensure that we must acknowledge and make a choice for the invalid case.

This now (in)famous blogpost on Go is what happens when you just pretend that everything works:

https://fasterthanli.me/articles/i-want-off-mr-golangs-wild-...

Re:Tracking Trust with Rust in the Kernel by johnisgood on Friday September 19, 2025 @08:12AM (#45300736)
OCaml, Haskell, Kotlin, and Swift has this, too...
Re:Tracking Trust with Rust in the Kernel by scns on Friday September 19, 2025 @09:57AM (#45301737)
There are at least some unikernels written in OCaml IIRC, don't know about kernells written in Haskell.
Re:Tracking Trust with Rust in the Kernel by johnisgood on Friday September 19, 2025 @10:18AM (#45301950)
Re:Tracking Trust with Rust in the Kernel by menaerus on Saturday September 20, 2025 @02:25AM (#45310950)
> We can't know if a generic path is valid at compile time. But we can at compile time ensure that we must acknowledge and make a choice for the invalid case.

While a pragmatic choice, in reality it isn't very practical IMO. Most of the times you really don't know what to do with the error so you end up turning the condition either into an exception, because it really is an exceptional case, and/or assert on it as an invariant that is broken and which you believe is a programming (usage) error. In any case, you don't really know how to handle it well, and this is I believe often the case for kernel design too - they will try to eliminate as much as possible such cases with testing because they can't afford to panick during the runtime because that would actually be detrimental for QoS.

Even in C++ you have something not quite the same but similar with [[nodiscard]] qualifier but in practice I haven't really seen it being used much but when I did it was mostly annoying to deal with. It's like a hot potato that is being thrown across the API boundaries but nobody wants to deal with it.

Re:Tracking Trust with Rust in the Kernel by JackSlateur on Sunday September 21, 2025 @10:35AM (#45323101)
Long story short: rust is buggy because it cannot handle paths that are not unicode ? :)
Re:Tracking Trust with Rust in the Kernel by jeroenhd on Friday September 19, 2025 @07:15AM (#45300307)
That's not a language feature as much as it is a culture and standard library feature.

For instance, Java could do the same, but in practice conversion methods throw runtime exceptions that you won't be warned about and your code will randomly crash if conversions fail, except in some cases when it doesn't. For casting between numeric types the language does have some legacy cruft, but for web applications nothing is stopping you from doing this. Java's explicit exception system is great at forcing developers to deal with potential failures, but in practice Java developers chose not to deal with exceptions so often that exceptions now get hidden.

Rust does this stuff in the standard library and that gives you the advantage that you don't need to explain the concept and convince every developer that this is a good idea. The same way Java has optional nullability annotations that are rarely used in practice because not every developer feels like adding them, unlike languages where nullability is part of the type system.

The concept is not exclusive to Rust, but I also haven't found it very popular outside of Rust developer circles.

Re:Tracking Trust with Rust in the Kernel by baq on Friday September 19, 2025 @08:01AM (#45300624)
I'm happily using this pattern in TS to wrap DB ids to avoid mistakenly passing e.g. user id to a function accepting a thing id or vice versa. Works very well and prevented a few close calls. I'd call it a type system feature, especially if you pack the id in a nominal type.
Re:Tracking Trust with Rust in the Kernel by cleartext412 on Saturday September 20, 2025 @08:16AM (#45312691)
> Java's explicit exception system is great at forcing developers to deal with potential failures, but in practice Java developers chose not to deal with exceptions so often that exceptions now get hidden.

Every time I see someone bringing up the idea of adding checked exceptions to a language (usually in these endless exceptions vs returning errors debates), it is met with "it won't work, look at Java", and it feels like a real shame. I'm sure complications of additional syntax would pay off just as fast as it does for regular type annotations.

Re:Tracking Trust with Rust in the Kernel by MBCook on Friday September 19, 2025 @09:18AM (#45301371)
Since I do web apps Strings are my bread and butter.

If you make UntrustedString a subclass of String (trusted) then you lose type safety. So TrustedString has to be the subclass. Easy enough.

But now string literals are “untrusted”. So you have to do new String(“this is trusted content”) everywhere you need it, which is a pain.

And you can’t add trusted strings. The operator will return a normal String (untrusted) so you have to cast it. Same with any function you call like substring.

So you have to live with that, or make overrides for every single string function that fix the types where necessary.

It’s just really non-ergonomic. I think having it built in would likely make it far better.

Re:Tracking Trust with Rust in the Kernel by lock1 on Friday September 19, 2025 @02:35PM (#45304903)

  > But now string literals are “untrusted”. So you have to do new String(“this is trusted content”) everywhere you need it, which is a pain.
Eh? Isn't that the main point of doing all of this? Being explicit on the boundaries but still providing a way to manipulate them like a String?

I don't see anything wrong with `new Validated("literal")` (or functional friendly `Validated.of("literal")`). If you intend to create a `Validated`, then create it via constructor / static factory method that enforces necessary validations to create `Validated`.

  > So you have to live with that, or make overrides for every single string function that fix the types where necessary.
Like `Optional<T>` and `Stream<T>`, you could define `Validated::map(Function<? super String,String>)` if you want. With `map()`, you could operate `Validated` with anything that accepts `String` like usual.

With that said, I don't recommend using `Validated` in your actual Spring project though, use (OOP) value objects instead. I used value objects quite a lot on my legacy Spring project. It plays nicely with functional-style, cover "validation" stuff, and avoiding primitive obsession. Putting `String` on `UserId` will result in a loud compiler error.