Skip to content

Commit

Permalink
Use edition 2021, fix lints, fmt code (#9)
Browse files Browse the repository at this point in the history
* Use edition 2021, fix lints, fmt code

Update linked-hash-map min-version

* Add github actions CI, trim license, rm travis
  • Loading branch information
alexheretic authored Dec 21, 2024
1 parent a24b7a5 commit 72e842c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 104 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Rust

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest
env:
RUST_BACKTRACE: 1
steps:
- run: rustup update stable
- uses: actions/checkout@v4
- run: cargo test --all-features

rustfmt:
runs-on: ubuntu-latest
steps:
- run: rustup update stable
- uses: actions/checkout@v4
- run: cargo fmt -- --check
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
name = "linked_hash_set"
version = "0.1.4"
authors = ["Alex Butler <alexheretic@gmail.com>"]
edition = "2018"
edition = "2021"
description = "HashSet with insertion ordering"
repository = "https://github.com/alexheretic/linked-hash-set"
keywords = ["data-structures"]
license = "Apache-2.0"
readme="README.md"

[dependencies]
linked-hash-map = "0.5.3"
linked-hash-map = "0.5.6"
serde = { version = "1", optional = true }

[dev-dependencies]
Expand Down
25 changes: 0 additions & 25 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,3 @@
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
25 changes: 0 additions & 25 deletions release

This file was deleted.

84 changes: 56 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ pub mod serde;

use linked_hash_map as map;
use linked_hash_map::{Keys, LinkedHashMap};
use std::borrow::Borrow;
use std::collections::hash_map::RandomState;
use std::fmt;
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::{Chain, FromIterator};
use std::ops::{BitAnd, BitOr, BitXor, Sub};
use std::{
borrow::Borrow,
collections::hash_map::RandomState,
fmt,
hash::{BuildHasher, Hash, Hasher},
iter::{Chain, FromIterator},
ops::{BitAnd, BitOr, BitXor, Sub},
};

// Note: This implementation is adapted from std `HashSet` implementation ~2017-10
// parts relying on std `HashMap` functionality that is not present in `LinkedHashMap` or
Expand Down Expand Up @@ -116,11 +118,8 @@ use std::ops::{BitAnd, BitOr, BitXor, Sub};
/// ```
/// use linked_hash_set::LinkedHashSet;
///
/// fn main() {
/// let viking_names: LinkedHashSet<&str> =
/// ["Einar", "Olaf", "Harald"].iter().cloned().collect();
/// // use the values stored in the set
/// }
/// let viking_names: LinkedHashSet<&str> = ["Einar", "Olaf", "Harald"].iter().cloned().collect();
/// // use the values stored in the set
/// ```
///
/// [`front()`]: struct.LinkedHashSet.html#method.front
Expand Down Expand Up @@ -514,10 +513,11 @@ where
/// assert_eq!(set.contains(&1), true);
/// assert_eq!(set.contains(&4), false);
/// ```
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
Q: ?Sized,
{
self.map.contains_key(value)
}
Expand All @@ -541,10 +541,11 @@ where
/// assert_eq!(was_refreshed, true);
/// assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![1, 3, 2]);
/// ```
pub fn refresh<Q: ?Sized>(&mut self, value: &Q) -> bool
pub fn refresh<Q>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
Q: ?Sized,
{
self.map.get_refresh(value).is_some()
}
Expand Down Expand Up @@ -707,10 +708,11 @@ where
/// assert_eq!(set.remove(&2), true);
/// assert_eq!(set.remove(&2), false);
/// ```
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
pub fn remove<Q>(&mut self, value: &Q) -> bool
where
T: Borrow<Q>,
Q: Hash + Eq,
Q: ?Sized,
{
self.map.remove(value).is_some()
}
Expand Down Expand Up @@ -865,7 +867,7 @@ where
}
}

impl<'a, 'b, T, S> BitOr<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
impl<T, S> BitOr<&LinkedHashSet<T, S>> for &LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
Expand Down Expand Up @@ -897,7 +899,7 @@ where
}
}

impl<'a, 'b, T, S> BitAnd<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
impl<T, S> BitAnd<&LinkedHashSet<T, S>> for &LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
Expand Down Expand Up @@ -929,7 +931,7 @@ where
}
}

impl<'a, 'b, T, S> BitXor<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
impl<T, S> BitXor<&LinkedHashSet<T, S>> for &LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
Expand Down Expand Up @@ -961,7 +963,7 @@ where
}
}

impl<'a, 'b, T, S> Sub<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
impl<T, S> Sub<&LinkedHashSet<T, S>> for &LinkedHashSet<T, S>
where
T: Eq + Hash + Clone,
S: BuildHasher + Default,
Expand Down Expand Up @@ -1141,7 +1143,7 @@ impl<'a, K> Iterator for Iter<'a, K> {
self.iter.size_hint()
}
}
impl<'a, K> ExactSizeIterator for Iter<'a, K> {
impl<K> ExactSizeIterator for Iter<'_, K> {
fn len(&self) -> usize {
self.iter.len()
}
Expand All @@ -1152,7 +1154,7 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
}
}

impl<'a, K: fmt::Debug> fmt::Debug for Iter<'a, K> {
impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
Expand Down Expand Up @@ -1252,7 +1254,7 @@ where
}
}

impl<'a, T, S> fmt::Debug for Intersection<'a, T, S>
impl<T, S> fmt::Debug for Intersection<'_, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
Expand Down Expand Up @@ -1297,7 +1299,7 @@ where
}
}

impl<'a, T, S> fmt::Debug for Difference<'a, T, S>
impl<T, S> fmt::Debug for Difference<'_, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
Expand Down Expand Up @@ -1330,7 +1332,7 @@ where
}
}

impl<'a, T, S> fmt::Debug for SymmetricDifference<'a, T, S>
impl<T, S> fmt::Debug for SymmetricDifference<'_, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
Expand All @@ -1348,7 +1350,7 @@ impl<'a, T, S> Clone for Union<'a, T, S> {
}
}

impl<'a, T, S> fmt::Debug for Union<'a, T, S>
impl<T, S> fmt::Debug for Union<'_, T, S>
where
T: fmt::Debug + Eq + Hash,
S: BuildHasher,
Expand Down Expand Up @@ -1813,10 +1815,7 @@ mod test_linked {
#[test]
fn clone_order_is_maintained() {
let set = set![123, 234, 56, 677];
assert_eq!(
set.clone().into_iter().collect::<Vec<_>>(),
vec![123, 234, 56, 677]
);
assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![123, 234, 56, 677]);
}

#[test]
Expand Down Expand Up @@ -1871,4 +1870,33 @@ mod test_linked {
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
}

#[test]
fn linked_set_equality() {
let mut set1 = LinkedHashSet::new();
assert!(set1.insert(234));
assert!(set1.insert(123));
assert!(set1.insert(345));

let mut set2 = LinkedHashSet::new();
assert!(set2.insert(123));
assert!(set2.insert(345));
assert!(set2.insert(234));

assert_eq!(set1, set2);

/// Returns true if the given sets are equal and have identical iteration order.
fn equal_and_same_order(a: &LinkedHashSet<i32>, b: &LinkedHashSet<i32>) -> bool {
a.len() == b.len() && a.iter().eq(b)
}

assert!(!equal_and_same_order(&set1, &set2));

let mut set3 = LinkedHashSet::new();
assert!(set3.insert(234));
assert!(set3.insert(123));
assert!(set3.insert(345));

assert!(equal_and_same_order(&set1, &set3));
}
}
18 changes: 11 additions & 7 deletions src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! An optional implementation of serialization/deserialization.
use super::LinkedHashSet;
use serde::de::{Error, SeqAccess, Visitor};
use serde::ser::SerializeSeq;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::{Formatter, Result as FmtResult};
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;
use crate::LinkedHashSet;
use serde::{
de::{Error, SeqAccess, Visitor},
ser::SerializeSeq,
Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
fmt::{Formatter, Result as FmtResult},
hash::{BuildHasher, Hash},
marker::PhantomData,
};

impl<K, S> Serialize for LinkedHashSet<K, S>
where
Expand Down

0 comments on commit 72e842c

Please sign in to comment.