Skip to content

Commit

Permalink
Add wasm32_web_time feature (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Jan 19, 2025
1 parent 717757e commit 177ce9e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ all-features = true
default = ["text"]
text = []
inline = ["text"]

wasm32_web_time = ["web-time"]
unicode = ["text", "unicode-segmentation", "bstr?/unicode", "bstr?/std"]
bytes = ["bstr", "text"]

Expand All @@ -35,6 +35,7 @@ serde_json = "1.0.68"
unicode-segmentation = { version = "1.7.1", optional = true }
bstr = { version = "1.5.0", optional = true, default-features = false }
serde = { version = "1.0.130", optional = true, features = ["derive"] }
web-time = { version = "1.1", optional = true }

[[example]]
name = "patience"
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/lcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//! * space `O(MN)`
use std::collections::BTreeMap;
use std::ops::{Index, Range};
use std::time::Instant;

use crate::algorithms::utils::{common_prefix_len, common_suffix_len, is_empty_range};
use crate::algorithms::DiffHook;
use crate::Instant;

/// LCS diff algorithm.
///
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub(crate) mod utils;

use std::hash::Hash;
use std::ops::{Index, Range};
use std::time::Instant;

use crate::Instant;
pub use capture::Capture;
pub use compact::Compact;
pub use hook::{DiffHook, NoFinishHook};
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/myers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
//! For potential improvements here see [similar#15](https://github.com/mitsuhiko/similar/issues/15).
use std::ops::{Index, IndexMut, Range};
use std::time::Instant;

use crate::algorithms::utils::{common_prefix_len, common_suffix_len, is_empty_range};
use crate::algorithms::DiffHook;
use crate::Instant;

/// Myers' diff algorithm.
///
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/patience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
//! by Pierre-Étienne Meunier.
use std::hash::Hash;
use std::ops::{Index, Range};
use std::time::Instant;

use crate::algorithms::{myers, DiffHook, NoFinishHook, Replace};
use crate::Instant;

use super::utils::{unique, UniqueItem};

Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::hash::Hash;
use std::ops::{Index, Range};
use std::time::Instant;

use crate::algorithms::{diff_deadline, Capture, Compact, Replace};
use crate::Instant;
use crate::{Algorithm, DiffOp};

/// Creates a diff between old and new with the given algorithm capturing the ops.
Expand Down
26 changes: 20 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@
//! a very, very long time to execute. Too long to make sense in practice.
//! To work around this issue all diffing algorithms also provide a version
//! that accepts a deadline which is the point in time as defined by an
//! [`Instant`](std::time::Instant) after which the algorithm should give up.
//! What giving up means depends on the algorithm. For instance due to the
//! recursive, divide and conquer nature of Myer's diff you will still get a
//! pretty decent diff in many cases when a deadline is reached. Whereas on the
//! other hand the LCS diff is unlikely to give any decent results in such a
//! situation.
//! [`Instant`] after which the algorithm should give up. What giving up means
//! depends on the algorithm. For instance due to the recursive, divide and
//! conquer nature of Myer's diff you will still get a pretty decent diff in
//! many cases when a deadline is reached. Whereas on the other hand the LCS
//! diff is unlikely to give any decent results in such a situation.
//!
//! The [`TextDiff`] type also lets you configure a deadline and/or timeout
//! when performing a text diff.
Expand All @@ -144,6 +143,10 @@
//! in a line diff. This currently also enables the `unicode` feature.
//! * `serde`: this feature enables serialization to some types in this
//! crate. For enums without payload deserialization is then also supported.
//! * `wasm32_web_time`: this feature swaps out the use of [`std::time`] for
//! the `web_time` crate. Because this is a change to the public interface,
//! this feature must be used with care. The instant type for this crate is
//! then re-exported top-level module.
#![warn(missing_docs)]
pub mod algorithms;
pub mod iter;
Expand All @@ -161,3 +164,14 @@ pub use self::common::*;
#[cfg(feature = "text")]
pub use self::text::*;
pub use self::types::*;

/// Internal alias for portability
#[cfg(not(feature = "wasm32_web_time"))]
pub(crate) use std::time::Instant;

/// WASM (browser) specific instant type.
///
/// This type is only available when the `wasm32_web_time` feature is enabled. In that
/// case this is an alias for [`web_time::Instant`].
#[cfg(feature = "wasm32_web_time")]
pub use web_time::Instant;
2 changes: 1 addition & 1 deletion src/text/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::fmt;

use crate::text::{DiffableStr, TextDiff};
use crate::types::{Algorithm, Change, ChangeTag, DiffOp, DiffTag};
use crate::Instant;
use crate::{capture_diff_deadline, get_diff_ratio};

use std::ops::Index;
use std::time::Instant;

use super::utils::upper_seq_ratio;

Expand Down
3 changes: 2 additions & 1 deletion src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::borrow::Cow;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::time::{Duration, Instant};
use std::time::Duration;

mod abstraction;
#[cfg(feature = "inline")]
Expand All @@ -17,6 +17,7 @@ use self::utils::{upper_seq_ratio, QuickSeqRatio};
use crate::algorithms::IdentifyDistinct;
use crate::iter::{AllChangesIter, ChangesIter};
use crate::udiff::UnifiedDiff;
use crate::Instant;
use crate::{capture_diff_deadline, get_diff_ratio, group_diff_ops, Algorithm, DiffOp};

#[derive(Debug, Clone, Copy)]
Expand Down

0 comments on commit 177ce9e

Please sign in to comment.