Interior mutability problem with glium


I’m playing with glium, trying to create small drawing framework. It seems that I cannot move forward without interior mutability. I have created very reduced project, which does nothing useful, just to narrow the problem. I would very appreciate any help on how to properly use Rc<RefCellglium::Display> in the code.

The error is:

error[E0277]: the trait bound std::cell::Ref<'_, glium::Display>: glium::backend::Facade is not satisfied
–> src\
36 | let vertex_buffer = glium::VertexBuffer::new(&display.borrow(), &shape).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait glium::backend::Facade is not implemented for std::cell::Ref<'_, glium::Display>
= note: required by <glium::VertexBuffer<T>>::new

Thanks in advance.

extern crate glium;

use std::cell::{RefCell};
use std::rc::Rc;

fn main() {
use glium::{glutin};

let mut events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new();
let context = glutin::ContextBuilder::new();

// this works
// let display = glium::Display::new(window, context, &events_loop).unwrap();

// this is causing a problem
let display = Rc::new(RefCell::new(glium::Display::new(window, context, &events_loop).unwrap()));

#[derive(Copy, Clone)]
struct Vertex {
    position: [f32; 2],

implement_vertex!(Vertex, position);

let vertex1 = Vertex { position: [-0.5, -0.5] };
let vertex2 = Vertex { position: [ 0.0,  0.5] };
let vertex3 = Vertex { position: [ 0.5, -0.25] };
let shape = vec![vertex1, vertex2, vertex3];

// this works
// let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();

// this is causing a problem
let vertex_buffer = glium::VertexBuffer::new(&display.borrow(), &shape).unwrap();



name = “delme”

version = “0.1.0”

edition = “2018”


glium = {version = “0.22.0”, features = [“glutin”]}


If you look at the error, it says that you don’t have a glium::Display and instead have a wrapper around it, so chances are that this works:

let vertex_buffer = glium::VertexBuffer::new(&*display.borrow(), &shape).unwrap();

Note the &*, this means we first dereference it and then take another reference, that is a standard &T reference.


That worked.
Thanks a lot.


Oh and also, for future reference, format your code like so:

  • Either add six spaces and a line before it

  • Or, even better surround it with three ` like so:


let your_code = here();


  • Which would look like:
let your_code = here();

That looks much better, I will definitely use it. Thanks again.