From de5262fcc97ddceedee98685f23fdec88ec5f754 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 28 Sep 2023 10:50:05 +0200 Subject: [PATCH 1/4] Add assertion --- crates/fj-core/src/objects/handles.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/fj-core/src/objects/handles.rs b/crates/fj-core/src/objects/handles.rs index d9ee8b6a5..fa6af5085 100644 --- a/crates/fj-core/src/objects/handles.rs +++ b/crates/fj-core/src/objects/handles.rs @@ -71,6 +71,8 @@ impl Handles { /// If the length of `Handles` is `i`, then retrieving the i-th edge using /// this method, is the same as retrieving the 0-th one. pub fn nth_circular(&self, index: usize) -> &Handle { + assert!(!self.is_empty(), "`Handles` must not be empty"); + let index = index % self.len(); self.nth(index) .expect("Index must be valid, due to modulo above") From d114e4375f06f6261a607d60a8778839ff7c5bb3 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 28 Sep 2023 10:50:56 +0200 Subject: [PATCH 2/4] Update doc comment --- crates/fj-core/src/objects/handles.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/fj-core/src/objects/handles.rs b/crates/fj-core/src/objects/handles.rs index fa6af5085..a17ba356a 100644 --- a/crates/fj-core/src/objects/handles.rs +++ b/crates/fj-core/src/objects/handles.rs @@ -70,6 +70,10 @@ impl Handles { /// /// If the length of `Handles` is `i`, then retrieving the i-th edge using /// this method, is the same as retrieving the 0-th one. + /// + /// # Panics + /// + /// Panics, if `Handles` is empty. pub fn nth_circular(&self, index: usize) -> &Handle { assert!(!self.is_empty(), "`Handles` must not be empty"); From 8f40971d94536499923661564c3809c3b09cf379 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 27 Sep 2023 10:38:10 +0200 Subject: [PATCH 3/4] Add `Handles::only` --- crates/fj-core/src/objects/handles.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/fj-core/src/objects/handles.rs b/crates/fj-core/src/objects/handles.rs index a17ba356a..eb4085ebc 100644 --- a/crates/fj-core/src/objects/handles.rs +++ b/crates/fj-core/src/objects/handles.rs @@ -61,6 +61,25 @@ impl Handles { self.inner.is_empty() } + /// Return the only item + /// + /// # Panics + /// + /// Panics, if there is more than one item. + pub fn only(&self) -> &Handle { + let mut iter = self.inner.iter(); + let item = iter + .next() + .expect("Requested only item, but no items available"); + + assert!( + iter.next().is_none(), + "Requested only item, but more than one available" + ); + + item + } + /// Return the n-th item pub fn nth(&self, index: usize) -> Option<&Handle> { self.inner.get(index) From 1f776e7ff9df1dd2fa34c370f9a84bd8f9ba5173 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 27 Sep 2023 10:47:32 +0200 Subject: [PATCH 4/4] Add `Handles::first` --- crates/fj-core/src/objects/handles.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/fj-core/src/objects/handles.rs b/crates/fj-core/src/objects/handles.rs index eb4085ebc..4db5a5cd0 100644 --- a/crates/fj-core/src/objects/handles.rs +++ b/crates/fj-core/src/objects/handles.rs @@ -80,6 +80,17 @@ impl Handles { item } + /// Return the first item + /// + /// # Panics + /// + /// Panics, if there are no items. + pub fn first(&self) -> &Handle { + self.inner + .first() + .expect("Requested first item, but no items available") + } + /// Return the n-th item pub fn nth(&self, index: usize) -> Option<&Handle> { self.inner.get(index)