Is there a way to fail at some branch of my match if the target is windows?

I use a library within my library and I have an enum to pass to this library however one of the variants fails on windows.
Is there a way for me to express this at compile time so people who try to compile for windows get the error at compile time.

enum MyEnum{
    WorksFine,
    FailsOnWindows
}

fn my_func(e:MyEnum){
	switch e {
		WorksFine=> doFine(),
		FailsOnWindows=>{
			//if windows fail at compiletime
			//else
			do_unix()
		}
	}
}

If you are writing a library, then don't do this, because it means that anyone that try to depend on it will have the compile error, even if they only use the library outside window. Add this to the top of lib.rs to make the library empty on windows:

#![cfg(not(windows))]

well i have to do this basically because of libcurl. On windows interface values cannot be device names only ip addresses but on other platforms theres no such issue.
So in my library if the user uses Name variant I want to prevent the compilation

You can apply attributes to specific variants, but you must document the portability hazard if you do

enum MyEnum{
    WorksFine,
    #[cfg(not(windows))]
    FailsOnWindows
}
2 Likes