Re: [PATCH v2 1/8] rust: fs: add file::Offset type alias

Danilo Krummrich
2025-10-21T16:25:19Z

On Tue Oct 21, 2025 at 6:00 PM CEST, Miguel Ojeda wrote:
> On Tue, Oct 21, 2025 at 5:26 PM Danilo Krummrich <dakr@kernel.org> wrote:
>>
>> Yeah, I don't think there's any value making this a new type in this case. There
>> are no type invariants, useful methods, etc.
>>
>> In fact, not even the type alias is strictly needed, as i64 would be sufficient
>> as well.
>
> Even if there are no type invariants nor methods, newtypes are still
> useful to prevent bad operations/assignments/...
>
> In general, it would be ideal to have more newtypes (and of course
> avoid primitives for everything), but they come with source code
> overhead, so I was wondering here, because it is usually one chance we
> have to try to go with something stronger vs. the usual C way.

We need arithmetic operations (e.g. checked_add()) and type conversions (e.g.
from/into usize). That'd be quite some code to write that just forwards to the
inner primitive.

So, I think as by now a new type makes sense if there is some reasonable
additional semantics to capture. Of course, if we'd have modular derive macros I
think it is reasonable and makes sense to also do it in cases like this one.

Maybe the action to take from this is to add this to the list of (good first)
issues (not sure this is actually a _good first_ issue though :).