mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Introduce a collections crate w/ deterministic hashmap, hashset in tests
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
5ec003530f
commit
1a91aa8194
10
Cargo.lock
generated
10
Cargo.lock
generated
@ -746,10 +746,10 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"arrayvec 0.7.1",
|
"arrayvec 0.7.1",
|
||||||
"clock",
|
"clock",
|
||||||
|
"collections",
|
||||||
"gpui",
|
"gpui",
|
||||||
"log",
|
"log",
|
||||||
"rand 0.8.3",
|
"rand 0.8.3",
|
||||||
"seahash",
|
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"sum_tree",
|
"sum_tree",
|
||||||
]
|
]
|
||||||
@ -1057,6 +1057,13 @@ dependencies = [
|
|||||||
"objc",
|
"objc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "collections"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"seahash",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "color_quant"
|
name = "color_quant"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@ -5739,6 +5746,7 @@ dependencies = [
|
|||||||
"async-tungstenite",
|
"async-tungstenite",
|
||||||
"base64 0.13.0",
|
"base64 0.13.0",
|
||||||
"clap 3.0.0-beta.2",
|
"clap 3.0.0-beta.2",
|
||||||
|
"collections",
|
||||||
"comrak",
|
"comrak",
|
||||||
"either",
|
"either",
|
||||||
"envy",
|
"envy",
|
||||||
|
@ -4,19 +4,19 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
test-support = ["rand", "seahash"]
|
test-support = ["rand"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clock = { path = "../clock" }
|
clock = { path = "../clock" }
|
||||||
|
collections = { path = "../collections" }
|
||||||
sum_tree = { path = "../sum_tree" }
|
sum_tree = { path = "../sum_tree" }
|
||||||
anyhow = "1.0.38"
|
anyhow = "1.0.38"
|
||||||
arrayvec = "0.7.1"
|
arrayvec = "0.7.1"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
rand = { version = "0.8.3", optional = true }
|
rand = { version = "0.8.3", optional = true }
|
||||||
seahash = { version = "4.1", optional = true }
|
|
||||||
smallvec = { version = "1.6", features = ["union"] }
|
smallvec = { version = "1.6", features = ["union"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
collections = { path = "../collections", features = ["test-support"] }
|
||||||
gpui = { path = "../gpui", features = ["test-support"] }
|
gpui = { path = "../gpui", features = ["test-support"] }
|
||||||
seahash = "4.1"
|
|
||||||
rand = "0.8.3"
|
rand = "0.8.3"
|
||||||
|
@ -12,6 +12,7 @@ mod tests;
|
|||||||
pub use anchor::*;
|
pub use anchor::*;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use clock::ReplicaId;
|
use clock::ReplicaId;
|
||||||
|
use collections::{HashMap, HashSet};
|
||||||
use operation_queue::OperationQueue;
|
use operation_queue::OperationQueue;
|
||||||
pub use point::*;
|
pub use point::*;
|
||||||
pub use point_utf16::*;
|
pub use point_utf16::*;
|
||||||
@ -31,31 +32,6 @@ use std::{
|
|||||||
pub use sum_tree::Bias;
|
pub use sum_tree::Bias;
|
||||||
use sum_tree::{FilterCursor, SumTree};
|
use sum_tree::{FilterCursor, SumTree};
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
|
||||||
#[derive(Clone, Default)]
|
|
||||||
pub struct DeterministicState;
|
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
|
||||||
impl std::hash::BuildHasher for DeterministicState {
|
|
||||||
type Hasher = seahash::SeaHasher;
|
|
||||||
|
|
||||||
fn build_hasher(&self) -> Self::Hasher {
|
|
||||||
seahash::SeaHasher::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
|
||||||
type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>;
|
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
|
||||||
type HashSet<T> = std::collections::HashSet<T, DeterministicState>;
|
|
||||||
|
|
||||||
#[cfg(not(any(test, feature = "test-support")))]
|
|
||||||
type HashMap<K, V> = std::collections::HashMap<K, V>;
|
|
||||||
|
|
||||||
#[cfg(not(any(test, feature = "test-support")))]
|
|
||||||
type HashSet<T> = std::collections::HashSet<T>;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Buffer {
|
pub struct Buffer {
|
||||||
fragments: SumTree<Fragment>,
|
fragments: SumTree<Fragment>,
|
||||||
|
10
crates/collections/Cargo.toml
Normal file
10
crates/collections/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "collections"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
test-support = ["seahash"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
seahash = { version = "4.1", optional = true }
|
26
crates/collections/src/lib.rs
Normal file
26
crates/collections/src/lib.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#[cfg(feature = "test-support")]
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
pub struct DeterministicState;
|
||||||
|
|
||||||
|
#[cfg(feature = "test-support")]
|
||||||
|
impl std::hash::BuildHasher for DeterministicState {
|
||||||
|
type Hasher = seahash::SeaHasher;
|
||||||
|
|
||||||
|
fn build_hasher(&self) -> Self::Hasher {
|
||||||
|
seahash::SeaHasher::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "test-support")]
|
||||||
|
pub type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>;
|
||||||
|
|
||||||
|
#[cfg(feature = "test-support")]
|
||||||
|
pub type HashSet<T> = std::collections::HashSet<T, DeterministicState>;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "test-support"))]
|
||||||
|
pub type HashMap<K, V> = std::collections::HashMap<K, V>;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "test-support"))]
|
||||||
|
pub type HashSet<T> = std::collections::HashSet<T>;
|
||||||
|
|
||||||
|
pub use std::collections::*;
|
@ -13,6 +13,7 @@ name = "seed"
|
|||||||
required-features = ["seed-support"]
|
required-features = ["seed-support"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
collections = { path = "../collections" }
|
||||||
rpc = { path = "../rpc" }
|
rpc = { path = "../rpc" }
|
||||||
|
|
||||||
anyhow = "1.0.40"
|
anyhow = "1.0.40"
|
||||||
@ -54,6 +55,7 @@ version = "0.5.2"
|
|||||||
features = ["runtime-async-std-rustls", "postgres", "time", "uuid"]
|
features = ["runtime-async-std-rustls", "postgres", "time", "uuid"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
collections = { path = "../collections", features = ["test-support"] }
|
||||||
gpui = { path = "../gpui" }
|
gpui = { path = "../gpui" }
|
||||||
zed = { path = "../zed", features = ["test-support"] }
|
zed = { path = "../zed", features = ["test-support"] }
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ use super::{
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use async_tungstenite::{tungstenite::protocol::Role, WebSocketStream};
|
use async_tungstenite::{tungstenite::protocol::Role, WebSocketStream};
|
||||||
|
use collections::{HashMap, HashSet};
|
||||||
use futures::{future::BoxFuture, FutureExt};
|
use futures::{future::BoxFuture, FutureExt};
|
||||||
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||||
use postage::{mpsc, prelude::Sink as _, prelude::Stream as _};
|
use postage::{mpsc, prelude::Sink as _, prelude::Stream as _};
|
||||||
@ -16,14 +17,7 @@ use rpc::{
|
|||||||
Connection, ConnectionId, Peer, TypedEnvelope,
|
Connection, ConnectionId, Peer, TypedEnvelope,
|
||||||
};
|
};
|
||||||
use sha1::{Digest as _, Sha1};
|
use sha1::{Digest as _, Sha1};
|
||||||
use std::{
|
use std::{any::TypeId, future::Future, mem, sync::Arc, time::Instant};
|
||||||
any::TypeId,
|
|
||||||
collections::{HashMap, HashSet},
|
|
||||||
future::Future,
|
|
||||||
mem,
|
|
||||||
sync::Arc,
|
|
||||||
time::Instant,
|
|
||||||
};
|
|
||||||
use store::{Store, Worktree};
|
use store::{Store, Worktree};
|
||||||
use surf::StatusCode;
|
use surf::StatusCode;
|
||||||
use tide::log;
|
use tide::log;
|
||||||
@ -220,7 +214,7 @@ impl Server {
|
|||||||
let receipt = request.receipt();
|
let receipt = request.receipt();
|
||||||
let host_user_id = self.state().user_id_for_connection(request.sender_id)?;
|
let host_user_id = self.state().user_id_for_connection(request.sender_id)?;
|
||||||
|
|
||||||
let mut contact_user_ids = HashSet::new();
|
let mut contact_user_ids = HashSet::default();
|
||||||
contact_user_ids.insert(host_user_id);
|
contact_user_ids.insert(host_user_id);
|
||||||
for github_login in request.payload.authorized_logins {
|
for github_login in request.payload.authorized_logins {
|
||||||
match self.app_state.db.create_user(&github_login, false).await {
|
match self.app_state.db.create_user(&github_login, false).await {
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use crate::db::{ChannelId, UserId};
|
use crate::db::{ChannelId, UserId};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
use collections::{HashMap, HashSet};
|
||||||
use rpc::{proto, ConnectionId};
|
use rpc::{proto, ConnectionId};
|
||||||
use std::collections::{hash_map, HashMap, HashSet};
|
use std::collections::hash_map;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Store {
|
pub struct Store {
|
||||||
@ -172,15 +173,15 @@ impl Store {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn contacts_for_user(&self, user_id: UserId) -> Vec<proto::Contact> {
|
pub fn contacts_for_user(&self, user_id: UserId) -> Vec<proto::Contact> {
|
||||||
let mut contacts = HashMap::new();
|
let mut contacts = HashMap::default();
|
||||||
for worktree_id in self
|
for worktree_id in self
|
||||||
.visible_worktrees_by_user_id
|
.visible_worktrees_by_user_id
|
||||||
.get(&user_id)
|
.get(&user_id)
|
||||||
.unwrap_or(&HashSet::new())
|
.unwrap_or(&HashSet::default())
|
||||||
{
|
{
|
||||||
let worktree = &self.worktrees[worktree_id];
|
let worktree = &self.worktrees[worktree_id];
|
||||||
|
|
||||||
let mut guests = HashSet::new();
|
let mut guests = HashSet::default();
|
||||||
if let Ok(share) = worktree.share() {
|
if let Ok(share) = worktree.share() {
|
||||||
for guest_connection_id in share.guests.keys() {
|
for guest_connection_id in share.guests.keys() {
|
||||||
if let Ok(user_id) = self.user_id_for_connection(*guest_connection_id) {
|
if let Ok(user_id) = self.user_id_for_connection(*guest_connection_id) {
|
||||||
|
Loading…
Reference in New Issue
Block a user