Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bitcode support #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rand = { version = "0.8", optional = true }
uuid = { version = "1.1", optional = true }
postgres-types = { version = "0.2.6", optional = true }
bytes = { version = "1.4.0", optional = true }
bitcode = { version = "0", features = ["derive"], default-features = false, optional = true }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should specify the minor version for major=0 versions.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying version = "0" allows the user to pick the major version of bitcode. Since ulid has reached version 1, it can't make breaking changes to update bitcode. For example if ulid added support for bitcode = "0.6" and bitcode = "0.7" was released, ulid couldn't update to it because that would be a breaking change.

bitcode = "0" allows any version the user wants to pick such as 0.5, 0.6 and future 0.7. The only tricky part is making sure ulid doesn't use APIs that will break between major versions. Luckily, bitcode docs specify that #[derive(bitcode::Encode, bitcode::Decode)] and feature = "derive" won't ever change between major versions.

While this method is unconventional, it allows 3rd party crates to support bitcode while allowing bitcode to remain unstable. One of bitcode's core goals is to keep improving its binary format. Unfortunately format changes are breaking changes so bitcode can't ever reach 1.0.


[target.wasm32-unknown-unknown.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand All @@ -31,6 +32,7 @@ web-time = "1"
[dev-dependencies]
bencher = "0.1"
serde_derive = "1.0"
bitcode = "0.5"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is extra, remove.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PrismaPhonic hi, thank you for your amazing pr! Would you be able to update this pull request (PR) when you have time?


[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub(crate) use bitmask;
/// Of the 128-bits, the first 48 are a unix timestamp in milliseconds. The
/// remaining 80 are random. The first 48 provide for lexicographic sorting and
/// the remaining 80 ensure that the identifier is unique.
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Clone, Copy)]
pub struct Ulid(pub u128);

Expand Down Expand Up @@ -422,3 +423,18 @@ mod tests {
println!("{}", DecodeError::InvalidChar);
}
}

#[cfg(all(test, feature = "std", feature = "bitcode"))]
mod bitcode_tests {
use super::*;
use bitcode;

#[test]
fn bitcode_roundtrip() {
let ulid = Ulid::new();
let encoded: Vec<u8> = bitcode::encode(&ulid).expect("failed to encode");
let decoded: Ulid = bitcode::decode(&encoded).expect("failed to decode");

assert_eq!(ulid, decoded);
}
}