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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
//! //! DAG Aware Artifact Builder //! ========================== //! //! Rust crate for managing the building of artifacts by builders which are //! connected in a directed acyclic graph (DAG) like manner. //! //! This crate provides essentially a cache which keeps artifacts of builders in //! order to prevent the same builder to produce multiple equal artifacts. //! This could be useful if the builders use consumable resources to create their //! artifacts, the building is a heavyweight procedure, or a given DAG dependency //! structure among the builders shall be properly preserved among their //! artifacts. //! //! The basic principal on which this crate is build, suggests two levels of //! abstraction, the builder level and the artifact level. Each builder type has //! one specific artifact type. The builders are represented by any struct, //! which implements the [`Builder`] trait, which in turn has an associate type //! that specifies the artifact type. //! //! `Builder`s are supposed to be wrapped in [`ArtifactPromise`]s, which prevents //! to call its `Builder::build()` method directly. In other respects, the //! `ArtifactPromise` acts a lot like an `Rc` and thus allows to share one //! instance among several dependants. //! This `Rc`-like structure creates naturally a DAG. //! //! For building a `Builder`, its `Builder::build()` method is provided with a //! [`ArtifactResolver`] that allows to resolve depending `ArtifactPromise`s into //! their respective artifacts, which is, in order to form a DAG, wrapped //! behind a `Rc`. //! //! As entry point serves the [`ArtifactCache`], which allows to resolve any //! `ArtifactPromise` to its artifact outside of a `Builder`. The //! `ArtifactCache` is essentially a cache. It can be used to translate any //! number of `ArtifactPromise`s, sharing their common dependencies. //! Consequently, resolving the same `ArtifactPromise` using the same //! `ArtifactCache` results in the same `Rc`ed artifact. //! //! When artifacts shall be explicitly recreated, e.g. to form a second //! independent artifact DAG, `ArtifactCache` has a `clear()` method //! to reset the cache. //! Additionally, `ArtifactCache` has an `invalidate()` method to remove a single //! builder artifact including its dependants (i.e. those artifacts which had //! used the invalidated one). //! //![`Builder`]: trait.Builder.html //![`ArtifactPromise`]: struct.ArtifactPromise.html //![`ArtifactResolver`]: struct.ArtifactResolver.html //![`ArtifactCache`]: struct.ArtifactCache.html //! //! //! //! ## Example //! //! ```rust //! use std::rc::Rc; //! use daab::*; //! //! // Simple artifact //! #[derive(Debug)] //! struct Leaf { //! //... //! } //! //! // Simple builder //! #[derive(Debug)] //! struct BuilderLeaf { //! // ... //! } //! impl BuilderLeaf { //! pub fn new() -> Self { //! Self { //! // ... //! } //! } //! } //! impl Builder for BuilderLeaf { //! type Artifact = Leaf; //! //! fn build(&self, _cache: &mut ArtifactResolver) -> Self::Artifact { //! Leaf{ //! // ... //! } //! } //! } //! //! // Composed artifact, linking to a Leaf //! #[derive(Debug)] //! struct Node { //! leaf: Rc<Leaf>, // Dependency artifact //! // ... //! } //! //! // Composed builder, depending on BuilderLeaf //! #[derive(Debug)] //! struct BuilderNode { //! builder_leaf: ArtifactPromise<BuilderLeaf>, // Dependency builder //! // ... //! } //! impl BuilderNode { //! pub fn new(builder_leaf: ArtifactPromise<BuilderLeaf>) -> Self { //! Self { //! builder_leaf, //! // ... //! } //! } //! } //! impl Builder for BuilderNode { //! type Artifact = Node; //! //! fn build(&self, cache: &mut ArtifactResolver) -> Self::Artifact { //! // Resolve ArtifactPromise to its artifact //! let leaf = cache.resolve(&self.builder_leaf); //! //! Node { //! leaf, //! // ... //! } //! } //! } //! //! // The cache to storing already created artifacts //! let mut cache = ArtifactCache::new(); //! //! // Constructing builders //! let leaf_builder = ArtifactPromise::new(BuilderLeaf::new()); //! //! let node_builder_1 = ArtifactPromise::new(BuilderNode::new(leaf_builder.clone())); //! let node_builder_2: ArtifactPromise<_> = BuilderNode::new(leaf_builder.clone()).into(); //! //! // Using the cache to access the artifacts from the builders //! //! // The same builder results in same artifact //! assert!(Rc::ptr_eq(&cache.get(&node_builder_1), &cache.get(&node_builder_1))); //! //! // Different builders result in different artifacts //! assert!( ! Rc::ptr_eq(&cache.get(&node_builder_1), &cache.get(&node_builder_2))); //! //! // Different artifacts may link the same dependent artifact //! assert!(Rc::ptr_eq(&cache.get(&node_builder_1).leaf, &cache.get(&node_builder_2).leaf)); //! ``` //! //! ## Debugging //! //! `daab` comes with extensive debugging gear. However, in order to //! keep the production impact as low as possible, the debugging facilityies //! are capsuled behind the `diagnostics` feature. //! //! Of course, the debugging feature is for the user of this crate to //! debug their graphs. Therefore, it is rather modeled as a //! diagnostics feature (hence the name). Consequently, the diagnosis //! is carried out by a [`Doctor`], which is a trait receiving various //! internal events in order to record them, print them, or otherwise help treating the bug. //! //! Care has been taken to keep the `diagnostics` feature broadly applicable as //! well as keeping the non-`diagnostics` API compatible with the //! `diagnostics`-API, meaning that a project using not using the //! `diagnostics` feature can be easily converted to using the //! `diagnostics`, usually by just replacing `ArtifactCache::new()` //! by `ArtifactCache::new_with_doctor()`. //! For this reason the `ArtifactCache` is generic to its doctor, which is //! important on its creation. The rest of the time the `ArtifactCache` //! uses `dyn Doctor` as its (fixed) generic argument. //! To ease conversion between them, all creatable `ArtifactCache`s //! (i.e. not `ArtifactCache<dyn Doctor>`) implement `DerefMut` to //! `ArtifactCache<dyn Doctor>` which has all the methods implemented. //! //![`Doctor`]: diagnostics/trait.Doctor.html //! //! //! ## Features //! //! This crate offers the following features: //! //! - **`diagnostics`** enables elaborate graph and cache interaction debugging. //! It adds the `new_with_doctor()` function to the `ArtifactCache` and adds //! the `diagnostics` module with the `Doctor` trait definition and some //! default `Doctor`s. //! //! - **`tynm`** enable the optional dependency on the [`tynm`] crate which adds //! functionality to abbreviate type names, which are used by some default //! `Doctor`s, hence it is only useful in connection with the `diagnostics` //! feature. //! //![`tynm`]: https://crates.io/crates/tynm //! // prevents compilation with broken Deref impl causing nasty stack overflows. #![deny(unconditional_recursion)] #![warn(missing_docs)] use std::rc::Rc; use std::collections::HashMap; use std::collections::HashSet; use std::any::Any; use std::hash::Hash; use std::hash::Hasher; use std::fmt::Debug; use std::borrow::Borrow; #[cfg(feature = "diagnostics")] use std::ops::Deref; #[cfg(feature = "diagnostics")] use std::ops::DerefMut; #[cfg(feature = "diagnostics")] pub mod diagnostics; #[cfg(feature = "diagnostics")] use diagnostics::Doctor; #[cfg(feature = "diagnostics")] use diagnostics::ArtifactHandle; #[cfg(feature = "diagnostics")] use diagnostics::BuilderHandle; #[cfg(feature = "diagnostics")] use diagnostics::NoopDoctor as DefDoctor; /// Represents a builder for an artifact. /// /// Each builder is supposed to contain all direct dependencies possibly other /// builders. /// In the `build()` function, the builder can access `cache` in order to /// resolve depending builders (as `ArtifactPromise`) in order to create their /// artifact. /// pub trait Builder: Debug { /// The artifact type as produced by this builder. /// type Artifact : Debug; /// Produces an artifact using the given `ArtifactResolver` for resolving /// dependencies. /// fn build(&self, cache: &mut ArtifactResolver) -> Self::Artifact; } /// Encapsulates a builder as promise for its artifact from the `ArtifactCache`. /// /// This struct is essentially a wrapper around `Rc<B>`, but it provides a /// `Hash` and `Eq` implementation based no the identity of the `Rc`s inner value. /// /// All clones of an `ArtifactPromise` are considered identical. /// /// An `ArtifactPromise` can be either resolved using the `ArtifactCache::get()` /// or `ArtifactResolver::resolve()`. /// #[derive(Debug)] pub struct ArtifactPromise<B: ?Sized> { builder: Rc<B>, id: BuilderId, } impl<B: Builder + 'static> ArtifactPromise<B> { /// Crates a new promise for the given builder. /// pub fn new(builder: B) -> Self { let builder = Rc::new(builder); let id = (&builder).into(); Self { builder, id, } } /// Changes the generic type of self to `dyn Any`. /// fn into_any(self) -> ArtifactPromise<dyn Any> where B: 'static { ArtifactPromise { builder: self.builder, id: self.id, } } } impl<B: ?Sized> Borrow<BuilderId> for ArtifactPromise<B> { fn borrow(&self) -> &BuilderId { &self.id } } impl<B: ?Sized> Clone for ArtifactPromise<B> { fn clone(&self) -> Self { ArtifactPromise { builder: self.builder.clone(), id: self.id, } } } impl Hash for ArtifactPromise<dyn Any> { fn hash<H: Hasher>(&self, state: &mut H) { self.id.hash(state); } } impl PartialEq for ArtifactPromise<dyn Any> { fn eq(&self, other: &Self) -> bool { self.id.eq(&other.id) } } impl Eq for ArtifactPromise<dyn Any> { } impl<B: Builder + 'static> From<B> for ArtifactPromise<B> { fn from(b: B) -> Self { Self::new(b) } } /// Resolves any `ArtifactPromise` used to resolve the dependencies of builders. /// /// This struct records each resolution in order to keep track of dependencies. /// This is used for correct cache invalidation. /// pub struct ArtifactResolver<'a> { user: &'a BuilderEntry, cache: &'a mut ArtifactCache, #[cfg(feature = "diagnostics")] diag_builder: &'a BuilderHandle, } impl<'a> ArtifactResolver<'a> { /// Resolves the given `ArtifactPromise` into its `Artifact`. /// pub fn resolve<B: Builder + 'static>(&mut self, promise: &ArtifactPromise<B>) -> Rc<B::Artifact> { #[cfg(feature = "diagnostics")] { self.cache.do_resolve(self.user, self.diag_builder, promise) } #[cfg(not(feature = "diagnostics"))] { self.cache.do_resolve(self.user, promise) } } } /// Id to differentiate builder instances across types. /// /// Notice, this type simply wraps `*const` to the builder `Rc`s. /// Consequentially, a `BuilderId`s validity is limited to the life time of /// the respective `Builder`. /// #[derive(Clone, Debug, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] struct BuilderId(*const dyn Any); impl<B: Builder + 'static> From<&Rc<B>> for BuilderId { fn from(rc: &Rc<B>) -> Self { BuilderId(rc.as_ref() as &dyn Any as *const dyn Any) } } #[derive(Clone, Debug)] struct ArtifactEntry { value: Rc<dyn Any>, } impl ArtifactEntry { fn new<T: Any + Debug>(value: Rc<T>) -> Self { ArtifactEntry { value, } } } #[derive(Clone, Debug)] struct BuilderEntry { value: ArtifactPromise<dyn Any>, id: BuilderId, } impl BuilderEntry { fn new<T: Builder + Debug + 'static>(value: ArtifactPromise<T>) -> Self { let id = value.id; BuilderEntry { value: value.into_any(), id, } } } impl Hash for BuilderEntry { fn hash<H: Hasher>(&self, state: &mut H) { self.value.hash(state); } } impl PartialEq for BuilderEntry { fn eq(&self, other: &Self) -> bool { self.value.eq(&other.value) } } impl Eq for BuilderEntry { } impl Borrow<BuilderId> for BuilderEntry { fn borrow(&self) -> &BuilderId { &self.id } } /// Central structure to prevent dependency duplication on building. /// /// Notice the debugging version (activating the **`diagnostics`** feature) of this /// struct contains a debugging `Doctor`, which /// allows run-time inspection of various events. Therefore, `ArtifactCache` /// is generic to some `Doctor`. /// Thus, the `new()` function returns actually a `ArtifactCache<NoopDoctor>` /// and `new_with_doctor()` returns some `ArtifactCache<T>` those /// can be store in variables. /// /// However, since most of the code does not care about the concrete /// `Doctor` the default generic is `dyn Doctor`. /// To ease conversion between them, all creatable `ArtifactCache`s /// (i.e. not `ArtifactCache<dyn Doctor>`) implement `DerefMut` to /// `ArtifactCache<dyn Doctor>` which has all the methods implemented. /// pub struct ArtifactCache< #[cfg(feature = "diagnostics")] T: ?Sized = dyn Doctor> { /// Maps Builder-Capsules to their Artifact value cache: HashMap<ArtifactPromise<dyn Any>, ArtifactEntry>, /// Tracks the direct promise dependants of each promise dependants: HashMap<BuilderId, HashSet<BuilderId>>, /// The doctor for error diagnostics. #[cfg(feature = "diagnostics")] doctor: T, } #[cfg(feature = "diagnostics")] impl Default for ArtifactCache<DefDoctor> { fn default() -> Self { ArtifactCache::new() } } #[cfg(not(feature = "diagnostics"))] impl Default for ArtifactCache { fn default() -> Self { ArtifactCache::new() } } #[cfg(not(feature = "diagnostics"))] impl ArtifactCache { /// Creates a new empty cache. /// pub fn new() -> Self { Self { cache: HashMap::new(), dependants: HashMap::new(), } } } #[cfg(feature = "diagnostics")] impl ArtifactCache<DefDoctor> { /// Creates a new empty cache with a dummy doctor. /// pub fn new() -> Self { Self { cache: HashMap::new(), dependants: HashMap::new(), doctor: DefDoctor::default(), } } } #[cfg(feature = "diagnostics")] impl<T: Doctor + 'static> Deref for ArtifactCache<T> { type Target = ArtifactCache; fn deref(&self) -> &Self::Target { self } } #[cfg(feature = "diagnostics")] impl<T: Doctor + 'static> DerefMut for ArtifactCache<T> { fn deref_mut(&mut self) -> &mut Self::Target { self } } #[cfg(feature = "diagnostics")] impl<T: Doctor + 'static> ArtifactCache<T> { /// Creates new empty cache with given doctor for drop-in inspection. /// /// **Notice: This function is only available if the `diagnostics` feature has been activated**. /// pub fn new_with_doctor(doctor: T) -> Self { Self { cache: HashMap::new(), dependants: HashMap::new(), doctor, } } /// Returns a reference of the inner doctor. /// /// **Notice: This function is only available if the `diagnostics` feature has been activated**. /// pub fn get_doctor(&mut self) -> &mut T { &mut self.doctor } /// Consumes the `ArtifactCache` and returns the inner doctor. /// /// **Notice: This function is only available if the `diagnostics` feature has been activated**. /// pub fn into_doctor(self) -> T { self.doctor } } impl ArtifactCache { /// Resolves the artifact of `promise` and records dependency between `user` /// and `promise`. /// fn do_resolve<B: Builder + 'static>(&mut self, user: &BuilderEntry, #[cfg(feature = "diagnostics")] diag_builder: &BuilderHandle, promise: &ArtifactPromise<B>) -> Rc<B::Artifact> { let deps = self.get_dependants(&promise.clone().into_any()); if !deps.contains(user.borrow()) { deps.insert(user.id); } #[cfg(feature = "diagnostics")] self.doctor.resolve(diag_builder, &BuilderHandle::new(promise.clone())); self.get(promise) } /// Returns the vector of dependants of promise /// fn get_dependants(&mut self, promise: &ArtifactPromise<dyn Any>) -> &mut HashSet<BuilderId> { if !self.dependants.contains_key(promise.borrow()) { self.dependants.insert(*promise.borrow(), HashSet::new()); } self.dependants.get_mut(promise.borrow()).unwrap() } /// Get the stored artifact if it exists. /// fn lookup<B: Builder + 'static>(&self, builder: &ArtifactPromise<B>) -> Option<Rc<B::Artifact>> where <B as Builder>::Artifact: 'static { // Get the artifact from the hash map ensuring integrity self.cache.get(&builder.id).map( |ent| { // Ensure value type ent.value.clone().downcast() .expect("Cached Builder Artifact is of invalid type") } ) } /// Store given artifact for given builder. /// fn insert(&mut self, builder: BuilderEntry, artifact: ArtifactEntry) { // Insert artifact self.cache.insert( builder.value, artifact, ); } /// Gets the artifact of the given builder. /// /// This method looks up whether the artifact for the given builder is still /// present in the cache, or it will use the builder to build and store the /// artifact. /// /// Notice the given promise will be stored kept to prevent it from /// deallocating. `clear()` or `invalidate()` must be called in order to /// free those `Rc`s if required. /// pub fn get<B: Builder + 'static>(&mut self, promise: &ArtifactPromise<B>) -> Rc<B::Artifact> where <B as Builder>::Artifact: 'static { if let Some(rc) = self.lookup(promise) { rc } else { let ent = BuilderEntry::new(promise.clone()); #[cfg(feature = "diagnostics")] let diag_builder = BuilderHandle::new(promise.clone()); let rc = Rc::new(promise.builder.build(&mut ArtifactResolver { user: &ent, cache: self, #[cfg(feature = "diagnostics")] diag_builder: &diag_builder, })); #[cfg(feature = "diagnostics")] self.doctor.build(&diag_builder, &ArtifactHandle::new(rc.clone())); self.insert(ent, ArtifactEntry::new( rc.clone() )); rc } } /// Clears the entire cache including all kept builder and artifact `Rc`s. /// pub fn clear(&mut self) { self.cache.clear(); self.dependants.clear(); #[cfg(feature = "diagnostics")] self.doctor.clear(); } /// Auxiliary invalidation function using a `BuilderId`. /// fn invalidate_any(&mut self, builder: BuilderId) { if let Some(set) = self.dependants.remove(&builder) { for dep in set { self.invalidate_any(dep); } } self.cache.remove(&builder); } /// Clears cached artifact of the given builder and all depending artifacts. /// /// Depending artifacts are all artifacts which used the former during /// its building. The dependencies are automatically tracked using the /// `ArtifactResolver` struct. /// pub fn invalidate<B: Builder + 'static>(&mut self, promise: &ArtifactPromise<B>) { let any_promise = promise.clone().into_any(); self.invalidate_any(any_promise.id); #[cfg(feature = "diagnostics")] self.doctor.invalidate(&BuilderHandle::new(promise.clone())); } } // ----------- #[cfg(test)] mod test;