honeycomb_core/cmap/dim2/embed.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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
//! Attribute operations implementation
//!
//! This module contains code used to implement operations on the embedded data associated to the
//! map. This includes operations regarding vertices as well as (in the future) user-defined
//! generic attributes
// ------ IMPORT
use stm::{StmResult, Transaction};
use crate::prelude::{AttributeBind, AttributeUpdate, CMap2, Vertex2, VertexIdType};
use crate::{
attributes::{AttributeStorage, UnknownAttributeStorage},
geometry::CoordsFloat,
};
// ------ CONTENT
/// **Built-in vertex-related methods**
impl<T: CoordsFloat> CMap2<T> {
/// Return the current number of vertices.
#[must_use = "unused return value"]
pub fn n_vertices(&self) -> usize {
self.vertices.n_attributes()
}
#[allow(clippy::missing_errors_doc)]
/// Return the vertex associated to a given identifier.
///
/// # Return / Errors
///
/// This method is meant to be called in a context where the returned `Result` is used to
/// validate the transaction passed as argument. Errors should not be processed manually,
/// only processed via the `?` operator.
///
/// This method return a `Option` taking the following values:
/// - `Some(v: Vertex2)` if there is a vertex associated to this ID,
/// - `None` otherwise.
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds,
/// - the index cannot be converted to `usize`.
pub fn read_vertex(
&self,
trans: &mut Transaction,
vertex_id: VertexIdType,
) -> StmResult<Option<Vertex2<T>>> {
self.vertices.read(trans, vertex_id)
}
#[allow(clippy::missing_errors_doc)]
/// Replace the vertex associated to a given identifier and return its old value.
///
/// # Arguments
///
/// - `vertex_id: VertexIdentifier` -- Identifier of the vertex to replace.
/// - `vertex: impl Into<Vertex2>` -- New [`Vertex2`] value.
///
/// # Return / Errors
///
/// This method is meant to be called in a context where the returned `Result` is used to
/// validate the transaction passed as argument. Errors should not be processed manually,
/// only processed via the `?` operator.
///
/// The result contains an `Option` taking the following values:
/// - `Some(v: Vertex2)` if there was an old value,
/// - `None` otherwise.
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds,
/// - the index cannot be converted to `usize`.
pub fn write_vertex(
&self,
trans: &mut Transaction,
vertex_id: VertexIdType,
vertex: impl Into<Vertex2<T>>,
) -> StmResult<Option<Vertex2<T>>> {
self.vertices.write(trans, vertex_id, vertex.into())
}
#[allow(clippy::missing_errors_doc)]
/// Remove the vertex associated to a given identifier and return it.
///
/// # Return / Errors
///
/// This method is meant to be called in a context where the returned `Result` is used to
/// validate the transaction passed as argument. Errors should not be processed manually,
// /// only processed via the `?` operator.
///
/// The result contains an `Option` taking the following values:
/// - `Some(v: Vertex2)` if there was a value,
/// - `None` otherwise.
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds,
/// - the index cannot be converted to `usize`.
pub fn remove_vertex(
&self,
trans: &mut Transaction,
vertex_id: VertexIdType,
) -> StmResult<Option<Vertex2<T>>> {
self.vertices.remove(trans, vertex_id)
}
#[must_use = "unused return value"]
/// Read the vertex associated to a given identifier.
///
/// This variant is equivalent to `read_vertex`, but internally uses a transaction that will be
/// retried until validated.
pub fn force_read_vertex(&self, vertex_id: VertexIdType) -> Option<Vertex2<T>> {
self.vertices.force_read(vertex_id)
}
/// Replace the vertex associated to a given identifier and return its old value.
///
/// This variant is equivalent to `write_vertex`, but internally uses a transaction that will be
/// retried until validated.
pub fn force_write_vertex(
&self,
vertex_id: VertexIdType,
vertex: impl Into<Vertex2<T>>,
) -> Option<Vertex2<T>> {
self.vertices.force_write(vertex_id, vertex.into())
}
#[allow(clippy::must_use_candidate)]
/// Remove the vertex associated to a given identifier and return it.
///
/// This variant is equivalent to `remove_vertex`, but internally uses a transaction that will
/// be retried until validated.
pub fn force_remove_vertex(&self, vertex_id: VertexIdType) -> Option<Vertex2<T>> {
self.vertices.force_remove(vertex_id)
}
}
/// **Generic attribute-related methods**
impl<T: CoordsFloat> CMap2<T> {
#[allow(clippy::missing_errors_doc)]
/// Return the attribute `A` value associated to a given identifier.
///
/// The kind of cell `A` binds to is automatically deduced using its `AttributeBind`
/// implementation.
///
/// # Return / Errors
///
/// This method is meant to be called in a context where the returned `Result` is used to
/// validate the transaction passed as argument. Errors should not be processed manually,
/// only processed via the `?` operator.
///
/// This method return a `Option` taking the following values:
/// - `Some(a: A)` if there is a value associated to this ID,
/// - `None` otherwise, or if there is no storage for this kind of attribute in the map.
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds,
/// - the index cannot be converted to `usize`.
pub fn read_attribute<A: AttributeBind + AttributeUpdate>(
&self,
trans: &mut Transaction,
id: A::IdentifierType,
) -> StmResult<Option<A>> {
self.attributes.read_attribute::<A>(trans, id)
}
#[allow(clippy::missing_errors_doc)]
/// Replace the attribute `A` value associated to a given identifier and return its old value.
///
/// # Arguments
///
/// - `index: A::IdentifierType` -- Identifier of the cell's value to replace.
/// - `val: A` -- Attribute value.
///
/// # Return / Errors
///
/// This method is meant to be called in a context where the returned `Result` is used to
/// validate the transaction passed as argument. Errors should not be processed manually,
/// only processed via the `?` operator.
///
/// The result contains an `Option` taking the following values:
/// - `Some(a: A)` if there was an old value,
/// - `None` otherwise, or if there is no storage for this kind of attribute in the map.
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds,
/// - the index cannot be converted to `usize`.
pub fn write_attribute<A: AttributeBind + AttributeUpdate>(
&self,
trans: &mut Transaction,
id: A::IdentifierType,
val: A,
) -> StmResult<Option<A>> {
self.attributes.write_attribute::<A>(trans, id, val)
}
#[allow(clippy::missing_errors_doc)]
/// Remove the attribute `A` value associated to a given identifier and return it.
///
/// # Return / Errors
///
/// This method is meant to be called in a context where the returned `Result` is used to
/// validate the transaction passed as argument. Errors should not be processed manually,
/// only processed via the `?` operator.
///
/// The result contains an `Option` taking the following values:
/// - `Some(a: A)` if there was a value,
/// - `None` otherwise, or if there is no storage for this kind of attribute in the map.
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds,
/// - the index cannot be converted to `usize`.
pub fn remove_attribute<A: AttributeBind + AttributeUpdate>(
&self,
trans: &mut Transaction,
id: A::IdentifierType,
) -> StmResult<Option<A>> {
self.attributes.remove_attribute::<A>(trans, id)
}
/// Return the attribute `A` value associated to a given identifier.
///
/// This variant is equivalent to `read_attribute`, but internally uses a transaction that will be
/// retried until validated.
pub fn force_read_attribute<A: AttributeBind + AttributeUpdate>(
&self,
id: A::IdentifierType,
) -> Option<A> {
self.attributes.force_read_attribute::<A>(id)
}
/// Replace the attribute `A` value associated to a given identifier and return its old value.
///
/// This variant is equivalent to `write_attribute`, but internally uses a transaction that will be
/// retried until validated.
pub fn force_write_attribute<A: AttributeBind + AttributeUpdate>(
&self,
id: A::IdentifierType,
val: A,
) -> Option<A> {
self.attributes.force_write_attribute::<A>(id, val)
}
/// Remove the attribute `A` value associated to a given identifier and return it.
///
/// This variant is equivalent to `remove_attribute`, but internally uses a transaction that
/// will be retried until validated.
pub fn force_remove_attribute<A: AttributeBind + AttributeUpdate>(
&self,
id: A::IdentifierType,
) -> Option<A> {
self.attributes.force_remove_attribute::<A>(id)
}
// --- big guns
/// Remove the attribute `A`'s storage from the map.
///
/// This method is useful when implementing routines that uses attributes to run; Those can
/// then be removed before the final result is returned.
pub fn remove_attribute_storage<A: AttributeBind + AttributeUpdate>(&mut self) {
self.attributes.remove_storage::<A>();
}
}