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

Cannot create static HistoryBuffer #158

Closed
andresv opened this issue May 5, 2020 · 7 comments · Fixed by #198
Closed

Cannot create static HistoryBuffer #158

andresv opened this issue May 5, 2020 · 7 comments · Fixed by #198

Comments

@andresv
Copy link
Contributor

andresv commented May 5, 2020

With Vec it is possible to do this:

use heapless::Vec; // fixed capacity `std::Vec`
use heapless::consts::U8; // type level integer used to specify capacity
static mut XS: Vec<u8, U8> = Vec(heapless::i::Vec::new());
let xs = unsafe { &mut XS };

However HistoryBuffer is missing in heapless::i.

@birkenfeld
Copy link
Contributor

That's (currently) unavoidable since the T: Default bound can't be used there.

@andresv
Copy link
Contributor Author

andresv commented May 5, 2020

So currently I can create it only to stack?

@korken89
Copy link
Contributor

korken89 commented May 5, 2020

I will have a look at fixing this, it's was a miss in my review of the HistoryBuffer PR

@korken89
Copy link
Contributor

korken89 commented May 5, 2020

For now, you can put it in an option as a workaround.

@andresv
Copy link
Contributor Author

andresv commented May 5, 2020

Like this?

#[app(device = hal::stm32, peripherals = true)]
const APP: () = {
    struct Resources {
        ntc: &'static HistoryBuffer<u16, U100>,
    }

    #[init]
    fn init(mut ctx: init::Context) -> init::LateResources {
        // hack to make a static alloc
        static mut NTC: Option<HistoryBuffer::<u16, U100>> = None;
        unsafe {NTC = Some(HistoryBuffer::new())};

        init::LateResources {
            ntc: unsafe {NTC.as_mut().unwrap()},
        }
    }

@korken89
Copy link
Contributor

korken89 commented May 5, 2020

Ah no, I think this should work with RTFM:

#[app(device = hal::stm32, peripherals = true)]
const APP: () = {
    struct Resources {
        ntc: HistoryBuffer<u16, U100>,
    }

    #[init]
    fn init(mut ctx: init::Context) -> init::LateResources {
        init::LateResources {
            ntc: HistoryBuffer::new(),
        }
    }

@andresv
Copy link
Contributor Author

andresv commented May 5, 2020

Indeed, this is much better.

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

Successfully merging a pull request may close this issue.

3 participants