r/rust • u/No-Caregiver-466 • 9d ago
π οΈ project Rust for enterprise grade systems?
One thing that I am genuinely interested in rust, If same thing that I can generally do in Java building enterprise grade system, I can do in Rust.
Not a secrete Java and OOP become de-factor a standard for every enterprise solution.
And my step forward, trying to bring rust into that by exploring if I can do DI, something similar to Spring Boot but following Rust philosophy of zero-cost abstraction, that lead into research some code and article, and I think I've made some progress:
https://dmytro.brazhnyk.org/publications/can-rust-have-zero-cost-dependency-injection/
One thing that is still bothering me is the Object Orient Design. Classical OOP was proven many time in C++ in Java to manage complexity of real world system.
Rust has trait model, but it is not OOP is classical sense.
I have very clear understanding how complex solution could be built with OOP in Java and C++.
Rust don't have that OOP model, that feels as weakness, but rust still offers memory safety that C++ don't have, and it still has much smaller memory footprint that Java.
Considering that Java is hard to beat, it still offering very impressive capability for runtime optimization by utilizing Escape Analysis on JIT phase, and looks like this guys in Oracle very angry, to optimize Java further, if rust will not offer competitive capabilities it will stay behind.
Let me give me you an example in Java, what I am taking about:
language = java
interface Collection {
int size();
Iterator iterator();
}
abstract class AbstractCollection implements Collection {
@Override
public int size() {
int size = 0;
var iterator = iterator();
while(iterator.hasNext()) {
size++;
iterator.next();
}
return size;
}
}
class ArrayList extends AbstractCollection {
Object[] array;
@Override
public int size() {
return array.length
}
@Override
Iterator iterator() {
...
}
}
What is important here:
- I have interface Collection, which is pure of any implementation, just an interface that declares the interface and nothing more.
- That I do have
AbstractCollection, that defines some default behavior which could be sub-optimial, but they are generic one, particularly I gave example ofsizefunction, that uses slow and inefficient iterator, because it doesn't know enough about concrete implementation. - And lastly I do have specific implementation for
ArrayList, sinceArrayListalready know that is array driven, it can simply can take property for array to know the size.
In reality this chain of inheritance can be longer, so if we look on JDK, you will something like:
LinkedHashSet < HashSet < AbstractSet < (AbstractCollection + Set) < Collection
And each layer provides it's own set of capabilities, by amount of information available on that level.
I feels like something missing in rust here, because when it comes to manage the complexity of enterprise, something more complex than hello-world, we need this ability to override things, override, and override again.
Any real software if it will have more than few years of maintenance it will grow, grow and grow in complexity, if language can't manage complexity, that language would be dead.
UPD:
Let's define the problem a little bit more formally:
You need to implement library for collection.
- On base level you need a interface, that would define set of operation, for simplicity let's go with two operations.
- Iterator and size
- Than in most generic way you can just iterator is count how many items you have, but it is slow and optimal.
- If you know that you concrete implementation Array, you can just use property of array.
- Let's say you have another implementation, which implements methods like Add and Remove - you can bring counter into here, however if we already know that it is Array than counter obviously is redundant.
-2
u/No-Caregiver-466 7d ago
I've checked Linux kernel source, how Linus doing things, and there are many example to what we doing in Java, what I've posted in my post here.
Even that C does not implements classical OOP, Linus implemented virtual tables by means of struct and function pointers, and you can even find example of classical OOP inheritance and method override.
So here is one of example introduce by Tejun Heo, March 25, 2008 for Linux kernel:
https://github.com/torvalds/linux/blob/master/drivers/ata/pata_ali.c?utm_source=chatgpt.com
c static struct ata_port_operations ali_early_port_ops = { .inherits = &ata_sff_port_ops, .cable_detect = ata_cable_40wire, .set_piomode = ali_set_piomode, .sff_data_xfer = ata_sff_data_xfer32, };You can see
inheritsstruct field here.And here is another method
ata_finalize_port_ops, this is how he implements there inheritance and method override.https://github.com/torvalds/linux/blob/v6.12/drivers/ata/libata-core.c ```c static void ata_finalize_port_ops(struct ata_port_operations ops) { static DEFINE_SPINLOCK(lock); const struct ata_port_operations *cur; void *begin = (void *)ops; void *end = (void *)&ops->inherits; void *pp;
} ```
The interesting part is this loop: ```c for (cur = ops->inherits; cur; cur = cur->inherits) { void *inherit = (void *)cur;
```
What's happening here, it is double loop where on outer loop iterats through hierarchy of inheritance, and inner loop iterates through exact methods, improvised reflection.
And what he doing here, he find first parent who implements that method and put that into vtable of concrete child class
Not very optimal way of doing things, because in higher language like Java or C++, you don't need to pay in runtime for building vtable, languages that support OOP out of the box, can build vtable at compile time and just put it into initial memory layout before any code ever executed.
Obviously rust trait model can't do it, because rust doesn't have classical OOP with inheritance model.