Skip to content
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

Consider adding as_has_store() for OwnedStructure #277

Closed
smessmer opened this issue Jan 30, 2020 · 3 comments
Closed

Consider adding as_has_store() for OwnedStructure #277

smessmer opened this issue Jan 30, 2020 · 3 comments

Comments

@smessmer
Copy link
Contributor

smessmer commented Jan 30, 2020

I use this code to find my structures that can be a delivery destination for energy:

fn structure_has_space_for_more_energy(structure: &OwnedStructure) -> bool {
    if let Some(structure) = structure.as_has_store() {
        structure.store_free_capacity(Some(ResourceType::Energy)) > 0
    } else {
        false
    }
}

fn find_energy_destinations() -> Vec<Structure> {
    let destinations = creep.room()
        .ok_or(...)?
        .find(find::MY_STRUCTURES)
        .iter()
        .filter(|structure| (structure.structure_type() == StructureType::Extension || structure.structure_type() == StructureType::Spawn) && structure_has_space_for_more_energy(structure));
}

But unfortunately, this doesn't work because OwnedStructure does not have a .as_has_store() method. Only Structure does. OwnedStructure does seem to have a .as_structure() method to make it into a Structure, but that one doesn't work on &OwnedStructure. But I don't get a OwnedStructure, filter() only gives me &OwnedStructure. What is the recommended way of getting the free energy capacity of a structure I own?

@smessmer
Copy link
Contributor Author

.clone().as_structure() worked for me, closing this for now.

But I think it would be a better API if OwnedStructure had as_has_store() (and the other related functions) available without having to cast it to Structure.

@ASalvail
Copy link
Collaborator

I'll leave it open, just to track the suggestion.

@ASalvail ASalvail reopened this Jan 30, 2020
@ASalvail ASalvail changed the title How to get store from OwnedStructure? Consider adding as_has_store() for OwnedStructure Jan 30, 2020
@shanemadden
Copy link
Collaborator

I think we've got equivalent coverage in the bindgen releases with StructureObject::as_has_store, so marking this as fixed by #333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants