Struct astral_string::Name

pub struct Name<'system, H = BuildHasherDefault<Murmur3>> { /* private fields */ }
Expand description

A UTF-8 encoded, immutable string optimized for numeric suffixes.

Example

Name can be created from a literal string:

use astral::string::Name;

let name = Name::new("foo", &string_subsystem);
assert_eq!(name, "foo");

Representation

Name stores a StringId, a reference to a Subsystem, and an optional numeric suffix. When a new Name is created, it is first checked if the string already exists. If so, it gets the same index as the existing one. If not, a new entry is created.

The suffix is only used for reusing the same string multiple times when the string only differs at a numeric suffix. A suffix with leading zeros cannot be optimized!

Implementations

Creates a Text from the given string literal in the specified Subsystem.

Example
use astral::string::Name;

let name = Name::new("foo", &string_subsystem);
assert_eq!(name, name);

Converts a slice of bytes to a Name.

Name requires that it is valid UTF-8. from_utf8 checks to ensure that the bytes are valid UTF-8, and then does the conversion.

If you are sure that the byte slice is valid UTF-8, and you don’t want to incur the overhead of the validity check, there is an unsafe version of this function, from_utf8_unchecked, which has the same behavior but skips the check.

Errors

Returns Err if the slice is not UTF-8 with a description as to why the provided slice is not UTF-8.

See the docs for Utf8Error for more details on the kinds of errors that can be returned.

Examples

Basic usage:

use astral::string::Name;

// some bytes, in a vector
let sparkle_heart = &[240, 159, 146, 150];

// We know these bytes are valid, so just use `unwrap()`.
let sparkle_heart = Name::from_utf8(sparkle_heart, &string_subsystem).unwrap();

assert_eq!("💖", sparkle_heart);

Incorrect bytes:

use astral::string::Name;

// some invalid bytes, in a vector
let sparkle_heart = &[0, 159, 146, 150];

assert!(Name::from_utf8(sparkle_heart, &string_subsystem).is_err());

Converts a slice of bytes to a Name, including invalid characters.

Name requires that it is valid UTF-8. from_utf8 checks to ensure that the bytes are valid UTF-8. During this conversion, from_utf8_lossy will replace any invalid UTF-8 sequences with U+FFFD REPLACEMENT CHARACTER, which looks like this: �

If you are sure that the byte slice is valid UTF-8, and you don’t want to incur the overhead of the conversion, there is an unsafe version of this function, from_utf8_unchecked, which has the same behavior but skips the checks.

Examples

Basic usage:

use astral::string::Name;

// some bytes, in a vector
let sparkle_heart = vec![240, 159, 146, 150];

let sparkle_heart = Name::from_utf8_lossy(&sparkle_heart, &string_subsystem);

assert_eq!("💖", sparkle_heart);

Incorrect bytes:

use astral::string::Name;

// some invalid bytes
let input = b"Hello \xF0\x90\x80World";
let output = Name::from_utf8_lossy(input, &string_subsystem);

assert_eq!("Hello �World", output);

Converts a slice of bytes to a Name without checking that the string contains valid UTF-8.

See the safe version, from_utf8, for more details.

Safety

This function is unsafe because it does not check that the bytes passed to it are valid UTF-8. If this constraint is violated, it may cause memory unsafety issues with future users of the String, as the rest of the library assumes that Names are valid UTF-8.

Example
use astral::string::Name;

// some bytes, in a vector
let sparkle_heart = &[240, 159, 146, 150];

let sparkle_heart = unsafe {
    Name::from_utf8_unchecked(sparkle_heart, &string_subsystem)
};

assert_eq!("💖", sparkle_heart);

Decode a UTF-16 encoded slice into a Name, returning Err if the slice contains any invalid data.

Example
use astral::string::Name;

// 𝄞music
let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
          0x0073, 0x0069, 0x0063];
assert_eq!(Name::new("𝄞music", &string_subsystem),
           Name::from_utf16(v, &string_subsystem).unwrap());

// 𝄞mu<invalid>ic
let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
          0xD800, 0x0069, 0x0063];
assert!(Name::from_utf16(v, &string_subsystem).is_err());

Decode a UTF-16 encoded slice into a Name, replacing invalid data with the replacement character (U+FFFD).

Example
use astral::string::Name;

// 𝄞mus<invalid>ic<invalid>
let v = &[0xD834, 0xDD1E, 0x006d, 0x0075,
          0x0073, 0xDD1E, 0x0069, 0x0063,
          0xD834];

assert_eq!(Name::new("𝄞mus\u{FFFD}ic\u{FFFD}", &string_subsystem),
           Name::from_utf16_lossy(v, &string_subsystem));

Creates a Name directly from a StringId, and a number in the specified Subsystem.

Safety

The Subsystem must match the one, which were used to create the StringId.

Example
use std::num::NonZeroU32;

use astral::string::{Name, StringId};

let id = StringId::new("Hello, world!", &string_subsystem);
// safe because the subsystem is the same
let hello = unsafe { Name::from_raw_parts(id, NonZeroU32::new(10), &string_subsystem) };

assert_eq!(hello, "Hello, world!10");

Returns the underlying StringId.

The StringId will be the same, if the strings and the subsystem are equal or only differ at the numeric suffix.

Example
use astral::string::Name;

let name1 = Name::new("foo-123", &string_subsystem);
let name2 = Name::new("foo-456", &string_subsystem);

assert_ne!(name1, name2);
assert_eq!(name1.id(), name2.id());

Returns the string part of the Name.

Example
use astral::string::Name;

let s = Name::new("foo123", &string_subsystem);

assert_eq!("foo", s.string_part());

Returns the number part of the Name.

Examples

Basic usage:

use astral::string::Name;

let s = Name::new("foo123", &string_subsystem);

assert_eq!(123, s.number().unwrap().get());

Returns the string as Cow.

If the Name does not contain a numeric suffix, a Borrowed can be returned. Otherwise, Owned is used.

Example
use std::borrow::Cow;

use astral::string::Name;

let name = Name::new("foo", &string_subsystem);
assert_eq!(name.as_str(), Cow::Borrowed("foo"));

let name = Name::new("bar-10", &string_subsystem);
let cow: Cow<'_, str> = Cow::Owned(String::from("bar-10"));
assert_eq!(name.as_str(), cow);

Remember, than a digital suffix with leading zeros cannot be optimized:

use std::borrow::Cow;

use astral::string::Name;

let name = Name::new("hello-010", &string_subsystem);
assert_eq!(name.as_str(), Cow::Borrowed("hello-010"));

Returns true if this Name has a length of zero.

Returns false otherwise.

Examples

Basic usage:

use astral::string::Name;

let s = Name::new("foo", &string_subsystem);

assert!(!s.is_empty());
assert!(Name::new("", &string_subsystem).is_empty());

Returns the length of this Name, in bytes.

Examples

Basic usage:

use astral::string::Name;

let s = Name::new("foo", &string_subsystem);

assert_eq!(s.len(), 3);

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.