Bounded Static Derive More
This is an experimental fork of Bounded Static Derive.
This crate provides a ToStatic macro which can be used to derive implementations of
the ToBoundedStatic and
IntoBoundedStatic traits for all structand enum
that can be converted to a form that is bounded by 'static.
Motivation
This fork differs from the original implementation in that it provides instances for struct types that
have non-ToStatic fields as long as those fields can be cloned and have no lifetime parameters.
For example:
This would not compile with the original Bounded Static Derive, since the last three fields do not have ToStatic instances.
We could use that library to derive an instance for Category, but that still doesn't help us with the other two fields,
which we didn't define, and can't provide instances for.
The IntoBoundedStatic instances for all three of these types is just identity, and the ToBoundedStatic instance would be
clone. This fork uses those operations directly in the derived instances for Response instead of requiring the ToStatic
constraints.
Details
Any field where the type is not does not contain either a (non-static) lifetime or any of the struct's type parameters will
receive the implementations described in the previous section. This means the field's type must have a Clone instances,
and the implementation will be provided but will not compile if it does not.
Less derivation
This fork also provides fewer derivations than the original project in one respect. If a struct has no lifetime parameters or
type parameters, adding the ToStatic derivation will cause compilation to fail. This isn't necessary in any sense, but I
decided to add it to keep things clean and clear, since those derived instances would not be used by other derived instances,
anyway.
Missing pieces
There are several possible additions that are not (yet) provided here:
- Extended derivation for enums. Currently only structs are supported. Supporting enums would be trivial, but I don't need it at the moment, and prefer to keep the diff small.
- Opting out via attribute. I guess we could support using the
ToStaticinstances in cases where theClonetreatment would be applied. It feels to me like we should do this, but I don't know what it would actually accomplish, and I don't care enough to do it. - Decoupling the two implementations. We could provide
IntoBoundedStaticalone for structs with non-Clonefields (whereToBoundedStaticisn't possible using this approach). Again, I don't need that at the moment, and don't really care that it's missing. - Generic type parameters. The new parts of this fork don't touch fields that contain any of the struct's generic type parameters, but the base implementation will
still add
ToStaticconstraints on the derived instance's parameters for these. That means that if you have a struct with aTtype parameter, instantiating that withserde_json::Value, for example, will not give youToStaticinstances. This limitation is kind of annoying, but it's not a big deal for me at the moment, and I haven't really thought about whether it could be fixed.
License
bounded-static-derive-more is distributed under the terms of the Apache License (Version 2.0).
See LICENSE for details.