-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute for skipping field of struct in Debug derives? #37009
Comments
At minimum we could certainly do it in a library with Macros 1.1 (then it would need a different name like |
Yeah, that would be a step forward at least. Would be nice to have it work for the built-in Debug though, I think. |
I started such a library a few days ago that does that: https://github.com/mcarton/rust-derivative. |
Cool, thanks for sharing that. Is Macros 1.1 already fully implemented in nightly then? I may give it a go shortly.
|
I'm wondering with all the recent Macro 2.0 developments whether the derive attribute macro might become a crate of its own in the future, such that it could be enhanced without changes to the actual compiler. A feature like this would then be much easier to get implemented, I suspect. Thoughts, @jseyfried @Manishearth? |
Custom derives can already be implemented outside of the compiler. |
@sfackler You misunderstand. I mean the workings of the derive attribute macro itself. |
How would pulling |
It actually is. The stage1 compiler cannot depend on proc macros IIRC. I
suppose you could still make it work with some shimming of libproc_macro
and including it directly in libsyntax.
…On Feb 3, 2018 9:28 AM, "Steven Fackler" ***@***.***> wrote:
How would pulling libsyntax_ext::deriving into a different crate make
this feature easier to implement? Difficulty of implementation is not the
limiting factor here in any case.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#37009 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABivSM9JAEoWXYntq2eAop_hqxcPNJBLks5tQ9llgaJpZM4KQgfg>
.
|
But that would mean that deriving can't be pulled out to a proc macro, not that pulling it out to a proc macro would make it easier to add extra control attributes, right? |
I was thinking more of maintainability and neat, elegant separation of concerns. Not sure on the details of what @Manishearth just said, but that could be an interesting point. |
Yes.
…On Feb 3, 2018 9:47 AM, "Steven Fackler" ***@***.***> wrote:
But that would mean that deriving can't be pulled out to a proc macro, not
that pulling it out to a proc macro would make it easier to add extra
control attributes, right?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#37009 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABivSJDNTB2oca9ltELAfA1bGF__wJolks5tQ93NgaJpZM4KQgfg>
.
|
Is there no elegant way to get around this restriction? |
I'm not 100% clear on whether this restriction will be a problem here. @eddyb knows |
Kind of digging up a buried corpse, but the crate https://crates.io/crates/derivative does that with I think the issue can be closed with this. Thank you @mcarton! |
A way of skipping fields in #[derive(Debug)] would be particularly useful for structs that have to use PhantomData. |
The feature is also helpful for structs that have sensitive data like AWS s3 access_key_id & secret_access_key. |
For me, the problem often arises when using 3rd party libraries that don't implement Sometimes all I need is just a quick way to derive (While libraries exist which can do this, it would be nice to not have to rely on them) |
I think that the issue of deriving Regarding skipping fields, serde has a similar mechanism for serialization: #[derive(Serialize)
struct MyStruct {
// serialized always
name: String,
// serialized never
#[serde(skip)]
aws_secret: String,
// serialized when `Option::is_none(&my_struct.extra)` is `false`
#[serde(skip_serializing_if = "Option::is_none")]
extra: Option<String>
} Debug could work similarly: #[derive(Debug)
struct MyStruct {
// debug always
name: String,
// debug never
#[debug(skip)]
aws_secret: String,
// serialized when `Option::is_none(&my_struct.extra)` is `false`
#[debug(skip_if = "Option::is_none")]
extra: Option<String>
} The API is can be a bit simpler compared to |
@aljazerzen I think you misunderstand me. Implementing a 3rd party trait for a 3rd party type is not allowed due to the orphan rule (though I suppose a workaround would be possible). I'm not arguing to overturn the orphan rule (or overturn it for only 1 specific instance). I'm talking about skipping fields in my own types which derive debug, but can't derive debug, because a third party type doesn't. Which is related to what this issue is about (and could be solved by it) #[derive(Debug)]
struct MyType {
other_field: u64,
// this third party type didn't implement Debug, now I can't derive debug
// but I don't care about getting the details of ThirdPartyType, I just want to quickly debug MyType
// having to do a manual Debug impl just to skip it would be cumbersome. I just want to program!
//
// If I do want to show data for ThirdPartyType, I will manually implement it,
// but I don't need to nor want to right now
//
// the following would be nice
#[debug(skip)]
foo: ThirdPartyType
} |
Yes, I didn't understand what you meant. This is very related and a good argument for the feature. |
Alternatively, perhaps Can someone summarize the current (EOY '23) best workaround? |
I have several cases where my struct contains a binary byte slice with stuff that isn't interesting for debugging, or outright shouldn't be logged -- think crypto keys. Just because it implements |
Note that this is currently not possible, because deriving happens before type checking. |
Use derivative - an alternative derive for implementing |
We need this feature so much because we don't want to expose private keys and credentials when debugging. |
As an alternative, you can also achieve that by wrapping sensitive data into a |
No! That's cheating! We need a built-in thing and not a third party crate, btw! |
Derivative is effectively unmaintained, there are a couple alternatives but it really is needed built-in IMO |
Same use case here, it would be great to have such a |
This also goes as cheating but a wrapper type isn’t exactly much code: use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
struct NoDebug<T> {
inner: T,
}
impl<T> Debug for NoDebug<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("skipped").finish()
}
}
impl<T> From<T> for NoDebug<T> {
fn from(value: T) -> Self {
Self { inner: value }
}
}
impl<T> Deref for NoDebug<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for NoDebug<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<T> Default for NoDebug<T>
where
T: Default,
{
fn default() -> Self {
Self {
inner: T::default(),
}
}
} Whatever fields shouldn’t appear in the debugging output can be wrapped then: #[derive(Debug, Default)]
struct Debuggable {
hidden: NoDebug<PhantomData<i32>>,
visible: i32,
}
fn main() {
dbg!(Debuggable::default());
} This prints:
|
+1 this would be a super useful feature :) |
+3. This would ease: +1: debugging (skipping large, useless, or non-Debug fields) |
@barries If the difference between HIPAA compliance and non-compliance is a |
Given the prevalence of using debug for logging in-flight data, I disagree.
…On Mon, Sep 2, 2024 at 1:24 PM Josh Junon ***@***.***> wrote:
@barries <https://github.com/barries> If the difference between HIPAA
compliance and non-compliance is a #[derive(Debug)] then you *severely*
need to revisit your data security practices and threat model. Information
security should not be relevant to this particular conversation whatsoever;
this is not a security feature.
—
Reply to this email directly, view it on GitHub
<#37009 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEXVA75KFGTLRXLEYNNWE3ZUSNLTAVCNFSM4CSCA7QKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMZSGUYTENZVGQ4A>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
+1, this would be great for ergonomics. |
For people watching this ticket; the crate |
This is actually being actively worked on, somehow the issues don't seem to be linked in any way. |
Could we have something like this?
This could also feasibly be applied to other auto-derivations, of course (not just Debug).
The text was updated successfully, but these errors were encountered: