Skip to content

Instantly share code, notes, and snippets.

@oceantume
Last active March 6, 2022 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oceantume/9b68a281c959447bf7ca4d8b8db5a1dc to your computer and use it in GitHub Desktop.
Save oceantume/9b68a281c959447bf7ca4d8b8db5a1dc to your computer and use it in GitHub Desktop.
Lifetime issues with EntityCommands
use bevy_ecs::{prelude::*, system::EntityCommands};
use bevy_math::prelude::*;
use bevy_text::*;
use bevy_transform::hierarchy::BuildChildren;
use bevy_ui::{entity::*, *};
use super::TooltipTextUiNode;
pub struct TooltipBuilder<'a, 'w, 's> {
commands: &'a mut Commands<'w, 's>,
text: Option<Text>,
}
impl<'a, 'w, 's> TooltipBuilder<'a, 'w, 's> {
/// Creates a new tooltip builder
pub fn new(commands: &'a mut Commands<'w, 's>) -> TooltipBuilder<'a, 'w, 's> {
Self {
commands,
text: None,
}
}
/// Sets the text of the tooltip
pub fn with_text(&mut self, text: Text) -> &mut Self {
self.text = Some(text);
self
}
/// Consumes the builder, spawns the entity and returns the EntityCommands for the root node.
pub fn spawn<'b>(self) -> EntityCommands<'w, 's, 'b> {
let root = self
.commands
.spawn_bundle(NodeBundle {
style: Style {
position_type: PositionType::Absolute,
border: Rect::all(Val::Px(2.0)),
align_items: AlignItems::Center,
..Default::default()
},
..Default::default()
})
.id();
let text = self
.commands
.spawn_bundle(TextBundle {
style: Style {
margin: Rect::all(Val::Px(5.0)),
..Default::default()
},
text: self.text.unwrap_or_default(),
..Default::default()
})
.insert(TooltipTextUiNode(root))
.id();
self.commands.entity(root).push_children(&[text]);
self.commands.entity(root)
}
}
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src\tooltip\builder.rs:58:23
|
58 | self.commands.entity(root)
| ^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
--> src\tooltip\builder.rs:14:6
|
14 | impl<'a, 'w, 's> TooltipBuilder<'a, 'w, 's> {
| ^^
note: ...so that reference does not outlive borrowed content
--> src\tooltip\builder.rs:58:9
|
58 | self.commands.entity(root)
| ^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'b` as defined here...
--> src\tooltip\builder.rs:30:18
|
30 | pub fn spawn<'b>(self) -> EntityCommands<'w, 's, 'b> {
| ^^
note: ...so that the types are compatible
--> src\tooltip\builder.rs:58:9
|
58 | self.commands.entity(root)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `EntityCommands<'w, 's, 'b>`
found `EntityCommands<'_, '_, '_>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment