Fyrox icon indicating copy to clipboard operation
Fyrox copied to clipboard

Type convert bug in 'UiNode::cast()'

Open WangHoi opened this issue 4 years ago • 1 comments

Maybe there is a bug:

impl UiNode {
  pub fn cast<T: Control>(&self) -> Option<&T> {
      self.0.as_any().downcast_ref::<T>()
  }
}

To consider composition style inheritance, it should be

impl UiNode {
  pub fn cast<T: Control>(&self) -> Option<&T> {
      self.0
          .query_component(TypeId::of::<T>())
          .map(|a| a.downcast_ref::<T>())
          .flatten()
  }
}

WangHoi avatar Dec 13 '21 11:12 WangHoi

Hi, query_component was added after cast and I just didn't think that cast could be improved in this way. This seems to be a very good improvement 🤔

mrDIMAS avatar Dec 13 '21 13:12 mrDIMAS

I explained the difference of the methods in the docs - https://github.com/FyroxEngine/Fyrox/blob/master/fyrox-ui/src/node.rs#L90-L111

mrDIMAS avatar Jun 15 '23 19:06 mrDIMAS