Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

writeable

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

writeable - cargo Package Compare versions

Comparing version
0.4.1
to
0.5.0
+1
-1
.cargo_vcs_info.json
{
"git": {
"sha1": "bb50cc9a00cbde008e6a8ef98655cf9b05e6dd6b"
"sha1": "38f41b6e170c432e88d684ebd6b546e4ca837ebf"
},
"path_in_vcs": "utils/writeable"
}

@@ -20,3 +20,3 @@ // This file is part of ICU4X. For terms of use, please see the file

fn write_len(&self) -> LengthHint {
fn writeable_length_hint(&self) -> LengthHint {
LengthHint::exact(self.message.len())

@@ -26,2 +26,4 @@ }

writeable::impl_display_with_writeable!(WriteableMessage<'_>);
/// A sample type implementing Display

@@ -44,3 +46,2 @@ struct DisplayMessage<'s> {

c.bench_function("writeable/overview", |b| {
#[allow(clippy::suspicious_map)]
b.iter(|| {

@@ -106,3 +107,3 @@ // This benchmark runs to_string on short, medium, and long strings in one batch.

fn writeable_dyn_to_string(w: &impl Writeable) -> String {
let mut output = String::with_capacity(w.write_len().capacity());
let mut output = String::with_capacity(w.writeable_length_hint().capacity());
w.write_to(&mut output as &mut dyn fmt::Write)

@@ -109,0 +110,0 @@ .expect("impl Write for String is infallible");

@@ -42,5 +42,5 @@ # This file is automatically @generated by Cargo.

name = "bumpalo"
version = "3.10.0"
version = "3.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3"
checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d"

@@ -175,5 +175,5 @@ [[package]]

name = "either"
version = "1.7.0"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"

@@ -208,11 +208,11 @@ [[package]]

name = "icu_benchmark_macros"
version = "0.6.0"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "630329e6635e4e3f747fb91fa9c0c2b8c83c1f3a2e4e49a282eb497be8f86b46"
checksum = "6c867656f2d9c90b13709ac88e710a9d6afe33998c1dfa22384bab8804e8b3d4"
[[package]]
name = "itertools"
version = "0.10.3"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [

@@ -236,5 +236,5 @@ "either",

name = "js-sys"
version = "0.3.59"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2"
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
dependencies = [

@@ -252,5 +252,5 @@ "wasm-bindgen",

name = "libc"
version = "0.2.127"
version = "0.2.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b"
checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966"

@@ -302,5 +302,5 @@ [[package]]

name = "once_cell"
version = "1.13.0"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1"

@@ -315,5 +315,5 @@ [[package]]

name = "plotters"
version = "0.3.2"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9428003b84df1496fb9d6eeee9c5f8145cb41ca375eb0dad204328888832811f"
checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97"
dependencies = [

@@ -335,5 +335,5 @@ "num-traits",

name = "plotters-svg"
version = "0.3.2"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0918736323d1baff32ee0eade54984f6f201ad7e97d5cfb5d6ab4a358529615"
checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f"
dependencies = [

@@ -351,5 +351,5 @@ "plotters-backend",

name = "proc-macro2"
version = "1.0.43"
version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
checksum = "7bd7356a8122b6c4a24a82b278680c73357984ca2fc79a0f9fa6dea7dced7c58"
dependencies = [

@@ -391,5 +391,5 @@ "unicode-ident",

name = "rand_core"
version = "0.6.3"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [

@@ -467,5 +467,5 @@ "getrandom",

name = "serde"
version = "1.0.142"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e590c437916fb6b221e1d00df6e3294f3fccd70ca7e92541c475d6ed6ef5fee2"
checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b"

@@ -484,5 +484,5 @@ [[package]]

name = "serde_derive"
version = "1.0.142"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34b5b8d809babe02f538c2cfec6f2c1ed10804c0e5a6a041a049a4f5588ccc2e"
checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c"
dependencies = [

@@ -496,5 +496,5 @@ "proc-macro2",

name = "serde_json"
version = "1.0.83"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38dd04e3c8279e75b31ef29dbdceebfe5ad89f4d0937213c53f7d49d01b3d5a7"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [

@@ -508,5 +508,5 @@ "itoa 1.0.3",

name = "syn"
version = "1.0.99"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2"
dependencies = [

@@ -539,11 +539,11 @@ "proc-macro2",

name = "unicode-ident"
version = "1.0.3"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
[[package]]
name = "unicode-width"
version = "0.1.9"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"

@@ -569,5 +569,5 @@ [[package]]

name = "wasm-bindgen"
version = "0.2.82"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d"
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
dependencies = [

@@ -580,5 +580,5 @@ "cfg-if",

name = "wasm-bindgen-backend"
version = "0.2.82"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "662cd44805586bd52971b9586b1df85cdbbd9112e4ef4d8f41559c334dc6ac3f"
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
dependencies = [

@@ -596,5 +596,5 @@ "bumpalo",

name = "wasm-bindgen-macro"
version = "0.2.82"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b260f13d3012071dfb1512849c033b1925038373aea48ced3012c09df952c602"
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
dependencies = [

@@ -607,5 +607,5 @@ "quote",

name = "wasm-bindgen-macro-support"
version = "0.2.82"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be8e654bdd9b79216c2929ab90721aa82faf65c48cdf08bdc4e7f51357b80da"
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
dependencies = [

@@ -621,11 +621,11 @@ "proc-macro2",

name = "wasm-bindgen-shared"
version = "0.2.82"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]]
name = "web-sys"
version = "0.3.59"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed055ab27f941423197eb86b2035720b1a3ce40504df082cac2ecc6ed73335a1"
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
dependencies = [

@@ -669,3 +669,3 @@ "js-sys",

name = "writeable"
version = "0.4.1"
version = "0.5.0"
dependencies = [

@@ -672,0 +672,0 @@ "criterion",

@@ -15,3 +15,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO

name = "writeable"
version = "0.4.1"
version = "0.5.0"
authors = ["The ICU4X Project Developers"]

@@ -48,3 +48,3 @@ include = [

[dev-dependencies.icu_benchmark_macros]
version = "0.6"
version = "0.7"

@@ -51,0 +51,0 @@ [dev-dependencies.rand]

@@ -35,7 +35,14 @@ // This file is part of ICU4X. For terms of use, please see the file

fn write_len(&self) -> LengthHint {
LengthHint::exact(11) + self.0.write_len()
fn writeable_length_hint(&self) -> LengthHint {
LengthHint::exact(11) + self.0.writeable_length_hint()
}
}
impl<V: Writeable> fmt::Display for WriteableMessage<V> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.write_to(f)
}
}
fn main() {

@@ -42,0 +49,0 @@ icu_benchmark_macros::main_setup!();

@@ -14,6 +14,2 @@ # writeable [![crates.io](https://img.shields.io/crates/v/writeable)](https://crates.io/crates/writeable)

Types implementing Writeable have a defaulted [`write_to_string`](Writeable::write_to_string)
function. If desired, types implementing `Writeable` can manually implement `ToString`
to wrap `write_to_string`.
## Examples

@@ -39,3 +35,3 @@

fn write_len(&self) -> LengthHint {
fn writeable_length_hint(&self) -> LengthHint {
// "Hello, " + '!' + length of name

@@ -48,2 +44,6 @@ LengthHint::exact(8 + self.name.len())

assert_writeable_eq!(&message, "Hello, Alice!");
// Types implementing `Writeable` are recommended to also implement `fmt::Display`.
// This can be simply done by redirecting to the `Writeable` implementation:
writeable::impl_display_with_writeable!(WelcomeMessage<'_>);
```

@@ -50,0 +50,0 @@

@@ -16,4 +16,3 @@ // This file is part of ICU4X. For terms of use, please see the file

let mut i = buf.len();
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
#[allow(clippy::indexing_slicing)] // n < 10^i
while n != 0 {

@@ -28,4 +27,3 @@ i -= 1;

}
#[allow(clippy::indexing_slicing)]
// TODO(#1668) Clippy exceptions need docs or fixing.
#[allow(clippy::indexing_slicing)] // buf is ASCII
let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };

@@ -35,3 +33,3 @@ sink.write_str(s)

fn write_len(&self) -> $crate::LengthHint {
fn writeable_length_hint(&self) -> $crate::LengthHint {
$crate::LengthHint::exact(if *self == 0 {

@@ -82,5 +80,5 @@ 1

fn write_len(&self) -> $crate::LengthHint {
fn writeable_length_hint(&self) -> $crate::LengthHint {
$crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 })
+ self.unsigned_abs().write_len()
+ self.unsigned_abs().writeable_length_hint()
}

@@ -141,3 +139,3 @@ }

#[inline]
fn write_len(&self) -> LengthHint {
fn writeable_length_hint(&self) -> LengthHint {
LengthHint::exact(self.len())

@@ -170,3 +168,3 @@ }

#[inline]
fn write_len(&self) -> LengthHint {
fn writeable_length_hint(&self) -> LengthHint {
LengthHint::exact(self.len())

@@ -193,4 +191,4 @@ }

#[inline]
fn write_len(&self) -> LengthHint {
(*self).write_len()
fn writeable_length_hint(&self) -> LengthHint {
(*self).writeable_length_hint()
}

@@ -197,0 +195,0 @@

@@ -6,3 +6,3 @@ // This file is part of ICU4X. For terms of use, please see the file

// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
#![cfg_attr(not(test), no_std)]
#![cfg_attr(all(not(test), not(doc)), no_std)]
#![cfg_attr(

@@ -15,3 +15,3 @@ not(test),

clippy::panic,
// TODO(#1668): enable clippy::exhaustive_structs,
clippy::exhaustive_structs,
clippy::exhaustive_enums,

@@ -33,6 +33,2 @@ missing_debug_implementations,

//!
//! Types implementing Writeable have a defaulted [`write_to_string`](Writeable::write_to_string)
//! function. If desired, types implementing `Writeable` can manually implement `ToString`
//! to wrap `write_to_string`.
//!
//! # Examples

@@ -58,3 +54,3 @@ //!

//!
//! fn write_len(&self) -> LengthHint {
//! fn writeable_length_hint(&self) -> LengthHint {
//! // "Hello, " + '!' + length of name

@@ -67,2 +63,6 @@ //! LengthHint::exact(8 + self.name.len())

//! assert_writeable_eq!(&message, "Hello, Alice!");
//!
//! // Types implementing `Writeable` are recommended to also implement `fmt::Display`.
//! // This can be simply done by redirecting to the `Writeable` implementation:
//! writeable::impl_display_with_writeable!(WelcomeMessage<'_>);
//! ```

@@ -93,2 +93,3 @@ //!

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[non_exhaustive]
pub struct LengthHint(pub usize, pub Option<usize>);

@@ -101,3 +102,3 @@

/// This is the exact length from `write_to`.
/// `write_to` will use exactly n bytes.
pub fn exact(n: usize) -> Self {

@@ -107,3 +108,3 @@ Self(n, Some(n))

/// This is at least the length from `write_to`.
/// `write_to` will use at least n bytes.
pub fn at_least(n: usize) -> Self {

@@ -113,3 +114,3 @@ Self(n, None)

/// This is at most the length from `write_to`.
/// `write_to` will use at most n bytes.
pub fn at_most(n: usize) -> Self {

@@ -119,3 +120,3 @@ Self(0, Some(n))

/// The length from `write_to` is in between `n` and `m`.
/// `write_to` will use between `n` and `m` bytes.
pub fn between(n: usize, m: usize) -> Self {

@@ -135,10 +136,7 @@ Self(Ord::min(n, m), Some(Ord::max(n, m)))

/// fn pre_allocate_string(w: &impl Writeable) -> String {
/// String::with_capacity(w.write_len().capacity())
/// String::with_capacity(w.writeable_length_hint().capacity())
/// }
/// ```
pub fn capacity(&self) -> usize {
match self {
Self(lower_bound, None) => *lower_bound,
Self(_lower_bound, Some(upper_bound)) => *upper_bound,
}
self.1.unwrap_or(self.0)
}

@@ -153,2 +151,3 @@

#[derive(Clone, Copy, Debug, PartialEq)]
#[allow(clippy::exhaustive_structs)] // stable
pub struct Part {

@@ -172,3 +171,3 @@ pub category: &'static str,

pub trait Writeable {
/// Writes bytes to the given sink. Errors from the sink are bubbled up.
/// Writes a string to the given sink. Errors from the sink are bubbled up.
/// The default implementation delegates to `write_to_parts`, and discards any

@@ -210,6 +209,6 @@ /// `Part` annotations.

/// Returns a hint for the number of bytes that will be written to the sink.
/// Returns a hint for the number of UTF-8 bytes that will be written to the sink.
///
/// Override this method if it can be computed quickly.
fn write_len(&self) -> LengthHint {
fn writeable_length_hint(&self) -> LengthHint {
LengthHint::undefined()

@@ -224,3 +223,3 @@ }

///
/// To remove the `Cow` wrapper, call `.into_owned()`.
/// To remove the `Cow` wrapper, call `.into_owned()` or `.as_str()` as appropriate.
///

@@ -259,6 +258,4 @@ /// # Examples

fn write_to_string(&self) -> Cow<str> {
let mut output = String::with_capacity(self.write_len().capacity());
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
self.write_to(&mut output)
.expect("impl Write for String is infallible");
let mut output = String::with_capacity(self.writeable_length_hint().capacity());
let _ = self.write_to(&mut output);
Cow::Owned(output)

@@ -268,2 +265,21 @@ }

/// Implements [`Display`](core::fmt::Display) for types that implement [`Writeable`].
///
/// It's recommended to do this for every [`Writeable`] type, as it will add
/// support for `core::fmt` features like [`fmt!`](std::fmt),
/// [`print!`](std::print), [`write!`](std::write), etc.
#[macro_export]
macro_rules! impl_display_with_writeable {
($type:ty) => {
/// This trait is implemented for compatibility with [`fmt!`](alloc::fmt).
/// To create a string, [`Writeable::write_to_string`] is usually more efficient.
impl core::fmt::Display for $type {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
$crate::Writeable::write_to(&self, f)
}
}
};
}
/// Testing macros for types implementing Writeable. The first argument should be a

@@ -296,3 +312,3 @@ /// `Writeable`, the second argument a string, and the third argument (*_parts_eq only)

/// }
/// fn write_len(&self) -> LengthHint {
/// fn writeable_length_hint(&self) -> LengthHint {
/// LengthHint::exact(3)

@@ -302,2 +318,4 @@ /// }

///
/// writeable::impl_display_with_writeable!(Demo);
///
/// assert_writeable_eq!(&Demo, "foo");

@@ -319,3 +337,3 @@ /// assert_writeable_eq!(&Demo, "foo", "Message: {}", "Hello World");

assert_eq!(actual_str, $crate::Writeable::write_to_string(actual_writeable), $($arg)+);
let length_hint = $crate::Writeable::write_len(actual_writeable);
let length_hint = $crate::Writeable::writeable_length_hint(actual_writeable);
assert!(length_hint.0 <= actual_str.len(), $($arg)*);

@@ -328,2 +346,3 @@ if let Some(upper) = length_hint.1 {

/// See [`assert_writeable_eq`].
#[macro_export]

@@ -340,3 +359,3 @@ macro_rules! assert_writeable_parts_eq {

assert_eq!(actual_parts, $expected_parts, $($arg)+);
let length_hint = $crate::Writeable::write_len(actual_writeable);
let length_hint = $crate::Writeable::writeable_length_hint(actual_writeable);
assert!(length_hint.0 <= actual_str.len(), $($arg)+);

@@ -343,0 +362,0 @@ if let Some(upper) = length_hint.1 {

@@ -89,6 +89,8 @@ // This file is part of ICU4X. For terms of use, please see the file

///
/// fn write_len(&self) -> LengthHint {
/// fn writeable_length_hint(&self) -> LengthHint {
/// LengthHint::exact(self.0.len()) | LengthHint::exact(self.1.len())
/// }
/// }
///
/// writeable::impl_display_with_writeable!(NonDeterministicWriteable);
/// ```

@@ -95,0 +97,0 @@ fn bitor(self, other: LengthHint) -> Self {

@@ -20,3 +20,3 @@ // This file is part of ICU4X. For terms of use, please see the file

fn write_len(&self) -> LengthHint {
fn writeable_length_hint(&self) -> LengthHint {
LengthHint::exact(self.message.len())

@@ -26,2 +26,4 @@ }

writeable::impl_display_with_writeable!(WriteableMessage<'_>);
#[test]

@@ -28,0 +30,0 @@ fn test_basic() {

Sorry, the diff of this file is not supported yet