I am trying to load a model into glium, I am using crate obj-rs for this. My model works fine with this example. Unfortunately whenever I try to move code from the example to my project (which is basically code copied from here) I fail with an error:
error[E0277]: the trait bound `glium::Display: glium::backend::Facade` is not satisfied
--> src/main.rs:27:32
|
27 | let vb = obj.vertex_buffer(&display)?;
| ------------- ^^^^^^^^ the trait `glium::backend::Facade` is not implemented for `glium::Display`
| |
| required by a bound introduced by this call
|
= help: the trait `glium::backend::Facade` is implemented for `Rc<glium::context::Context>`
note: required by a bound in `obj::glium_support::<impl Obj<V, I>>::vertex_buffer`
--> /Users/user/project/obj-rs/src/lib.rs:309:33
|
309 | pub fn vertex_buffer<F: Facade>(
| ^^^^^^ required by this bound in `obj::glium_support::<impl Obj<V, I>>::vertex_buffer`
error[E0277]: the trait bound `glium::Display: glium::backend::Facade` is not satisfied
--> src/main.rs:28:31
|
28 | let ib = obj.index_buffer(&display)?;
| ------------ ^^^^^^^^ the trait `glium::backend::Facade` is not implemented for `glium::Display`
| |
| required by a bound introduced by this call
|
= help: the trait `glium::backend::Facade` is implemented for `Rc<glium::context::Context>`
note: required by a bound in `obj::glium_support::<impl Obj<V, I>>::index_buffer`
--> /Users/user/project/obj-rs/src/lib.rs:317:32
|
317 | pub fn index_buffer<F: Facade>(
| ^^^^^^ required by this bound in `obj::glium_support::<impl Obj<V, I>>::index_buffer`
I am new to rust so most of the time I don't know what I am doing. I find it really hard to simply make copied code work. I tried implementing Facade for Display but that didn't seem to make any difference, just more errors. Could you tell me what's wrong, or at least provide me with an example that works, I just want to add my own models to glium examples.
Here are the parts of code that I changed:
fn main() -> Result<(), Box<dyn std::error::Error>>{
#[allow(unused_imports)]
use glium::{glutin, Surface};
use obj::{load_obj, Obj};
use glium::glutin::dpi::LogicalSize;
let event_loop = glutin::event_loop::EventLoop::new();
let window = glutin::window::WindowBuilder::new()
.with_inner_size(LogicalSize::new(500.0, 400.0))
.with_title("obj-rs");
let context = glutin::ContextBuilder::new().with_depth_buffer(24);
let display = glium::Display::new(window, context, &event_loop)?;
let input = include_bytes!("model.obj");
let obj: Obj = load_obj(&input[..])?;
//two lines causing the problem
//let vb = obj.vertex_buffer(&display)?;
//let ib = obj.index_buffer(&display)?;
let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap();
..... rest is the same as in the glium example
One thing that I would like to differ from obj-rs example is not to use local source if you know what I mean. Basically something like this:
[dependencies]
glium = "*"
obj-rs = { version = "*", features = ["glium"] }
I am new to rust and embedded systems. I was reading and going along "The Discovery Book" and I can't seem to figure out the errors below.
I have placed this in my main.rs:
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use aux5::{entry, prelude::*, Delay, Leds};
#[entry]
fn main() -> ! {
let (mut delay, mut leds): (Delay, Leds) = aux5::init();
let half_period = 500_u16;
loop {
leds[0].on();
delay.delay_ms(half_period);
leds[0].off();
delay.delay_ms(half_period);
}
}
My auxiliary lib.rs contains:
pub use stm32f3_discovery::{leds::Leds, stm32f3xx_hal, switch_hal};
pub use switch_hal::{ActiveHigh, OutputSwitch, Switch, ToggleableOutputSwitch};
use stm32f3xx_hal::prelude::*;
pub use stm32f3xx_hal::{
delay::Delay,
gpio::{gpioe, Output, PushPull},
hal::blocking::delay::DelayMs,
stm32,
};
pub type LedArray = [Switch<gpioe::PEx<Output<PushPull>>, ActiveHigh>; 8];
pub fn init() -> (Delay, LedArray) {
let device_periphs = stm32::Peripherals::take().unwrap();
let mut reset_and_clock_control = device_periphs.RCC.constrain();
let core_periphs = cortex_m::Peripherals::take().unwrap();
let mut flash = device_periphs.FLASH.constrain();
let clocks = reset_and_clock_control.cfgr.freeze(&mut flash.acr);
let delay = Delay::new(core_periphs.SYST, clocks);
// initialize user leds
let mut gpioe = device_periphs.GPIOE.split(&mut reset_and_clock_control.ahb);
let leds = Leds::new(
gpioe.pe8,
gpioe.pe9,
gpioe.pe10,
gpioe.pe11,
gpioe.pe12,
gpioe.pe13,
gpioe.pe14,
gpioe.pe15,
&mut gpioe.moder,
&mut gpioe.otyper,
);
(delay, leds.into_array())
}
I have not changed anything in lib.rs and I am following the instructions from the book, along with the source code.
Running the code results in the following errors when building for the target:
error[E0432]: unresolved import `aux5::prelude`
--> src\05-led-roulette\src\main.rs:5:19
|
5 | use aux5::{entry, prelude::*, Delay, Leds};
| ^^^^^^^ could not find `prelude` in `aux5`
error[E0308]: mismatched types
--> src\05-led-roulette\src\main.rs:9:48
|
9 | let (mut delay, mut leds): (Delay, Leds) = aux5::init();
| ------------- ^^^^^^^^^^^^ expected struct `Leds`, found array of 8 elements
| |
| expected due to this
|
= note: expected tuple `(Delay, Leds)`
found tuple `(Delay, [Switch<PEx<Output<PushPull>>, ActiveHigh>; 8])`
error[E0608]: cannot index into a value of type `Leds`
--> src\05-led-roulette\src\main.rs:14:9
|
14 | leds[0].on();
| ^^^^^^^
error[E0599]: no method named `delay_ms` found for struct `Delay` in the current scope
--> src\05-led-roulette\src\main.rs:15:15
|
15 | delay.delay_ms(half_period);
| ^^^^^^^^ method not found in `Delay`
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
5 | use aux5::DelayMs;
|
error[E0608]: cannot index into a value of type `Leds`
--> src\05-led-roulette\src\main.rs:17:9
|
17 | leds[0].off();
| ^^^^^^^
error[E0599]: no method named `delay_ms` found for struct `Delay` in the current scope
--> src\05-led-roulette\src\main.rs:18:15
|
18 | delay.delay_ms(half_period);
| ^^^^^^^^ method not found in `Delay`
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
5 | use aux5::DelayMs;
|
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0308, E0432, E0599, E0608.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `led-roulette`
To learn more, run the command again with --verbose.
From the following page, "The Led and Delay abstractions", it looks like this is the set of imports that you need.
use aux5::{entry, Delay, DelayMs, LedArray, OutputSwitch};
In the above abstraction, compared to your main.rs, note the:
Addition of DelayMs trait, making .delay_ms() available.
Removal of a prelude as it is not being used.
Change from Leds to the expected LedArray.
Also see:
Stack Overflow, "Why do I need to import a trait to use the methods it defines for a type?"
This question already has answers here:
What does “`str` does not have a constant size known at compile-time” mean, and what's the simplest way to fix it?
(1 answer)
Return local String as a slice (&str)
(7 answers)
Proper way to return a new string in Rust
(2 answers)
Closed 3 years ago.
I am having problems in an exercise where I try to get an impl block to return the value of the i32 in the struct in str format. I don't know if it is doing well or if it is possible to do this, this is the complete error:
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:6:20
|
6 | fn x(&self) -> str {
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: the return type of a function must have a statically known size
error[E0308]: mismatched types
--> src/main.rs:9:9
|
6 | fn x(&self) -> str {
| --- expected `str` because of return type
...
9 | *_newword
| ^^^^^^^^^ expected `str`, found struct `std::string::String`
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:17:5
|
17 | println!("x is: {}", f.x());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
This is the code that I am trying to make work:
struct Foo<'a> {
x: &'a i32,
}
impl<'a> Foo<'a> {
fn x(&self) -> str {
let word = *self.x;
let _newword = &word.to_string();
*_newword
}
}
fn main() {
let y = &5;
let f = Foo { x: y };
println!("x is: {}", f.x());
}
This question already has answers here:
What are Rust's exact auto-dereferencing rules?
(4 answers)
Closed 4 years ago.
Which rule makes the following code work?
struct Dummy(i32);
impl Dummy {
pub fn borrow(&self) {
println!("{}", self.0);
}
}
fn main() {
let d = Dummy(1);
(&d).borrow();
d.borrow();
}
I expect d.borrow() won't work as it passes in self which doesn't match the method signature borrow(&self).
From the reference :
A method call consists of an expression (the receiver) followed by a
single dot, an expression path segment, and a parenthesized
expression-list
When looking up a method call, the receiver may be automatically
dereferenced or borrowed in order to call a method.
Note:
Automatic dereference or borrow is only valid for the receiver. If there is no expression as receiver it will not work. Compiler will expect the borrowed type.
Example:
fn main() {
let d = Dummy(1);
let borrowed = Dummy::borrow(d);
}
Compiler will show an error:
error[E0308]: mismatched types
--> src/main.rs:12:34
|
12 | let borrowed = Dummy::borrow(d);
| ^
| |
| expected &Dummy, found struct `Dummy`
| help: consider borrowing here: `&d`
|
= note: expected type `&Dummy`
found type `Dummy`
This question already has answers here:
Rust proper error handling (auto convert from one error type to another with question mark)
(5 answers)
How do you define custom `Error` types in Rust?
(3 answers)
Closed 4 years ago.
The following code
fn get_issues(addr: &str) -> Result<(), std::error::Error> {
let mut result = String::new();
reqwest::get(&format!("{}/issues", addr))?.read_to_string(&mut result)?;
unimplemented!();
}
yields
error[E0277]: the trait bound `std::error::Error + 'static: std::marker::Sized` is not satisfied
--> src/main.rs:37:1
|
37 | / fn get_issues(addr: &str) -> Result<(), std::error::Error> {
38 | | let mut result = String::new();
39 | | reqwest::get(&format!("{}/issues", addr))?.read_to_string(&mut result)?;
40 | |
41 | | unimplemented!();
42 | | }
| |_^ `std::error::Error + 'static` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `std::error::Error + 'static`
= note: required by `std::result::Result`
However, reqwest::get() returns Result<.., reqwest::Error> and read_to_string() returns Result<.., std::io::Error>. I would like to use some kind of common error type to avoid type errors.
Is there some kind of error type I can use to make get_issues work as-is or do I need to convert the errors to my own error type?