microcad_core/
traits.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! µcad core geometry traits
5
6use crate::{Integer, Rect};
7
8/// Trait to align something to center.
9pub trait Center<T = Self> {
10    /// Align geometry.
11    fn center(&self) -> T;
12}
13
14/// Trait to distribute geometries in a 2D grid.
15pub trait DistributeGrid<T = Self> {
16    /// Distribute in a grid.
17    fn distribute_grid(&self, rect: Rect, rows: Integer, columns: Integer) -> T;
18}
19
20/// Return total amount of memory in bytes.
21pub trait TotalMemory {
22    /// Total amount of memory in bytes.
23    fn total_memory(&self) -> usize {
24        self.stack_memory() + self.heap_memory()
25    }
26
27    /// Get amount of stack memory in bytes.
28    fn stack_memory(&self) -> usize {
29        std::mem::size_of_val(self)
30    }
31
32    /// Get amount of heap memory in bytes.
33    fn heap_memory(&self) -> usize {
34        0
35    }
36}
37
38impl<T> TotalMemory for Vec<T> {
39    fn heap_memory(&self) -> usize {
40        self.capacity() * std::mem::size_of::<T>()
41    }
42}
43
44/// Return number of vertices.
45pub trait VertexCount {
46    /// Return vertex count.
47    fn vertex_count(&self) -> usize;
48}