Can't migrate away from Foo::KIND #1073
-
Hi, I'm posting this as a question as I aknowledge this is due to a lack of knowledge on my side :/. I'm updating an old codebase and I have a function defined like this: pub async fn apply_resource<K: Resource + Serialize> (
name: &str,
data: K,
ns: &str,
) -> Result<bool> {
use std::{
fs::{self, File},
io::Write,
path::Path,
};
// some stuff happening here
// Old version
debug!("Writing {} CRD for {} to {}", K::KIND, name, pth.display());
// TODO: fix the kind, this line isn't working
debug!("Writing {} CRD for {} to {}", K::kind(&()), name, pth.display()); In this migration guide here: https://github.com/kube-rs/kube/blob/main/CHANGELOG.md#kube-derive, it's mentioned that However in my case I don't really have a well defined As it's defined above, I'm getting this error message:
Would you know how to resolve that? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Ah, yeah, writing generics on top of kube's The key part that you are trying to interact with is this bit of the If you were just calling this in a non-generic setting you'd be fine with just passing in an empty tuple as the generic associated type Thus you need to constrain the See for instance: kube/kube-runtime/src/controller/mod.rs Lines 483 to 488 in f6ca7df |
Beta Was this translation helpful? Give feedback.
-
I tried this: pub async fn apply_resource<K: Resource + Serialize> (
name: &str,
data: K,
ns: &str,
) -> Result<bool>
where
K::DynamicType: Default,
{
use std::{
fs::{self, File},
io::Write,
path::Path,
};
// some stuff happening here
// TODO: fix the kind, this line isn't working
debug!("Writing {} CRD for {} to {}", K::kind(Default::default()), name, pth.display()); But it didn't work:
However this form does compile: pub async fn apply_resource<K: Resource<DynamicType = ()> + Serialize> (
name: &str,
data: K,
ns: &str,
) -> Result<bool>
where
K::DynamicType: Default,
{
use std::{
fs::{self, File},
io::Write,
path::Path,
};
// TODO: fix the kind
debug!("Writing {} CRD for {} to {}", K::kind(&()), name, pth.display()); Note: I initially had the function implemented for K with Does that make sense? |
Beta Was this translation helpful? Give feedback.
Ah, yeah, writing generics on top of kube's
Resource
traits is currently a little tricky with the associated type.The key part that you are trying to interact with is this bit of the
Resource
trait.If you were just calling this in a non-generic setting you'd be fine with just passing in an empty tuple as the generic associated type
DynamicType
(which is the right type for the static k8s-openapi types), but because you are writing a generic function, you are also trying to cover all possible resources (such asDynamicObject
andObject
in kube, or other Resource implementors).Thus you need to constrain the
DynamicType
to the subset you want (i.e. where it is just the empty tuple). The wa…