Hi,
I am following through some vulkan tutorials and try to an implement a struct new function creating the struct elements. Intent is to of course create reusable code.
se std::sync::Arc;
use vulkano::device::{Device, DeviceExtensions, Features, Queue};
use vulkano::instance::InstanceCreationError;
use vulkano::instance::{Instance, PhysicalDevice, QueueFamily};
struct Halstate<'a> {
instance: Box<Arc<Instance>>,
phy_device: PhysicalDevice<'a>,
queue_family: QueueFamily<'a>,
logical_device: Arc<Device>,
queue: Arc<Queue>,
}
impl<'a> Halstate<'a> {
fn new() -> Halstate<'a> {
let required_extensions = vulkano_win::required_extensions();
// setup vulkan with required_extensions ; type InstanceExtension
// This sets the following to true ; all else are false
// khr_surface
// khr_win32_surface
// khr_get_physical_device_properties
// khr_get_surface_capabilities2
let instance = match Instance::new(None, &required_extensions, None) {
Ok(instance) => Box::new(instance),
Err(err) => match err {
InstanceCreationError::IncompatibleDriver => panic!("Incompatible Driver"),
_ => panic!("unknown error"),
},
};
// List the available devices
for phy_dev in PhysicalDevice::enumerate(&instance) {
println!("Using device: {} (type:{:?})", phy_dev.name(), phy_dev.ty());
}
let phy_device = PhysicalDevice::enumerate(&instance)
.next()
.expect("no device available");
// for the physical device list the available queue families and give the number of queues
// they support
for qf in phy_device.queue_families() {
println!("Found a queue family with {:?} queue(s)", qf.queues_count());
}
// Choose first queue family that supports graphics
let qf = phy_device
.queue_families()
.find(|&q| q.supports_graphics())
.expect("couldn't find a graphical queue family");
// open a logical device and acquire a list of its queues
let (logical_device, mut queues) = {
Device::new(
phy_device,
&Features::none(),
&DeviceExtensions::none(),
[(qf, 0.5)].iter().cloned(),
)
.expect("failed to create device")
};
println!("{:?}", logical_device);
// use first available queue for the device
let queue = queues.next().unwrap();
println!("{:?}", queue);
Halstate {
instance: instance,
phy_device,
queue_family: qf,
logical_device,
queue,
}
// Surface supports queue_family
// Instance dropped here; vk.DestroyInstance
}
}
fn main() {
Halstate::new();
}
//#[allow(dead_code)]
from this I get an error particularly about returning a local variable.
error[E0515]: cannot return value referencing local variable `instance`
--> init-command-buffer\src\main.rs:78:9
|
40 | let phy_device = PhysicalDevice::enumerate(&instance)
| --------- `instance` is borrowed here
...
78 | / Halstate {
79 | | instance: instance,
80 | | phy_device,
81 | | queue_family: qf,
82 | | logical_device,
83 | | queue,
84 | | }
| |_________^ returns a value referencing data owned by the current function
error[E0505]: cannot move out of `instance` because it is borrowed
--> init-command-buffer\src\main.rs:79:23
|
15 | impl<'a> Halstate<'a> {
| -- lifetime `'a` defined here
...
40 | let phy_device = PhysicalDevice::enumerate(&instance)
| --------- borrow of `instance` occurs here
...
78 | / Halstate {
79 | | instance: instance,
| | ^^^^^^^^ move out of `instance` occurs here
80 | | phy_device,
81 | | queue_family: qf,
82 | | logical_device,
83 | | queue,
84 | | }
| |_________- returning this value requires that `instance` is borrowed for `'a`
I think I understand the problem but I don't understand the solution. I thought adding Box and lifetimes for instance would help but so far no luck.
Any ideas?
Thanks,
Frank