forma/composition/
state.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::cell::{RefCell, RefMut};
use std::mem;
use std::rc::Rc;

use rustc_hash::FxHashMap;
use surpass::painter::Props;
use surpass::{GeomId, LinesBuilder, Order};

use super::interner::Interner;

#[derive(Debug)]
pub struct LayerSharedStateInner {
    pub lines_builder: Option<LinesBuilder>,
    pub geom_id_to_order: FxHashMap<GeomId, Option<Order>>,
    pub props_interner: Interner<Props>,
    geom_id_generator: GeomId,
}

impl Default for LayerSharedStateInner {
    fn default() -> Self {
        Self {
            lines_builder: Some(LinesBuilder::default()),
            geom_id_to_order: FxHashMap::default(),
            props_interner: Interner::default(),
            geom_id_generator: GeomId::default(),
        }
    }
}

impl LayerSharedStateInner {
    pub fn new_geom_id(&mut self) -> GeomId {
        let prev = self.geom_id_generator;
        mem::replace(&mut self.geom_id_generator, prev.next())
    }
}

#[derive(Debug, Default)]
pub struct LayerSharedState {
    inner: Rc<RefCell<LayerSharedStateInner>>,
}

impl LayerSharedState {
    pub fn new(inner: Rc<RefCell<LayerSharedStateInner>>) -> Self {
        Self { inner }
    }

    pub fn inner(&mut self) -> RefMut<'_, LayerSharedStateInner> {
        self.inner.borrow_mut()
    }
}

impl PartialEq<Rc<RefCell<LayerSharedStateInner>>> for LayerSharedState {
    fn eq(&self, other: &Rc<RefCell<LayerSharedStateInner>>) -> bool {
        Rc::ptr_eq(&self.inner, other)
    }
}

// Safe as long as `inner` can only be accessed by `&mut self`.
unsafe impl Sync for LayerSharedState {}