serde icon indicating copy to clipboard operation
serde copied to clipboard

Support non-string-literal wrapped paths

Open Nemo157 opened this issue 1 year ago • 3 comments

The current path-taking attributes like #[serde(crate = ...)] require the path to be wrapped in a string literal. I believe this is due to a limitation in older versions of the compiler, but it's possible to pass paths in directly now.

This causes issues when trying to use paths like #[serde(crate = $crate::reexports::serde)], the $crate token cannot be serialized through a string.

I think it would be backwards compatible to extend these attributes to parse either a string literal or a directly passed path to support these kinds of usecases.

Nemo157 avatar Nov 27 '24 14:11 Nemo157

Looking back at the original implementation (https://github.com/serde-rs/serde/pull/1499) I see there was some discussion about workarounds like:

use $crate::reexports::serde as _serde;
...
#[serde(crate = "_serde")]

But it would be nice if these weren't needed.

Nemo157 avatar Nov 27 '24 14:11 Nemo157

Hmm... would this require serde_derive to be compiler-version dependent, too? We can either have a one-time cost of a build script, or a cost at every invocation, but if we can just accept paths without first needing to check the rust version, that would be ok.

oli-obk avatar Nov 27 '24 15:11 oli-obk

I have the feeling that it shouldn't require any version dependence (but I've not tried this sort of code on old compilers). For old compilers it's just not possible to have non-literal tokens in this position, so attempting to parse as a path will always fail, but there's nothing stopping you from trying that parse.

Nemo157 avatar Nov 27 '24 16:11 Nemo157

It would be so helpful if this was resolved. Should I make a PR?

janTatesa avatar Dec 12 '25 08:12 janTatesa