Struct screeps::local::ObjectId

source ·
pub struct ObjectId<T> { /* private fields */ }
Expand description

Represents an Object ID and a type that the ID points to, stored in Rust memory.

Use JsObjectId if a reference stored in JavaScript memory is preferred.

Each object ID in Screeps: World is represented by an ID of up to 24 hexidemical characters, which cannot change. This implementation takes advantage of that by storing a packed representation in a u128, using 96 bits for the ID and 32 bits for tracking the length of the ID string for reconstruction in JS.

Conversion

Use into to convert between ObjectId<T> and RawObjectId, and ObjectId::into_type to change the type this ObjectId points to freely.

Ordering

To facilitate use as a key in a BTreeMap or other similar data structures, ObjectId implements PartialOrd and Ord.

ObjectId’s are ordered by the corresponding order of their underlying byte values. This agrees with:

  • lexicographical ordering of the object id strings
  • JavaScript’s ordering of object id strings
  • ordering of RawObjectIds

Note: when running on the official screeps server, or on a private server backed by a MongoDB database, this ordering roughly corresponds to creation order. The first four bytes of a MongoDB-created ObjectId are seconds since the epoch when the id was created, so up to a second accuracy, these ids will be sorted by object creation time.

Implementations§

source§

impl<T> ObjectId<T>

source

pub fn into_type<U>(self) -> ObjectId<U>

Changes the type this ObjectId points to, unchecked.

This will allow changing to any type - ObjectId makes no guarantees about its ID matching the type of any object in the game that it actually points to.

source

pub fn from_packed(packed: u128) -> Self

Creates an object ID from its packed representation.

The input to this function is the bytes representing the up-to-24 hex digits in the object id.

See also RawObjectId::from_packed.

source

pub fn to_u128(self) -> u128

Converts this object ID to a u128 number.

The returned number, when formatted as hex, will produce a string parseable into this object id.

The returned number will be less than or equal to 2^96 - 1, as that’s the maximum value that RawObjectId can hold.

source

pub fn to_array_string(&self) -> ArrayString<24>

Formats this object ID as a string on the stack.

This is equivalent to ToString::to_string, but involves no allocation.

See also RawObjectId::to_array_string.

source

pub fn try_resolve(self) -> Result<Option<T>, RoomObject>
where T: MaybeHasId + JsCast,

Resolves this object ID into an object, verifying that the returned object matches the expected type.

Errors

Will return an error if this ID’s type does not match the object it points to with the resolved RoomObject with an unknown type.

Will return Ok(None) if the object no longer exists, or is in a room we don’t have vision for.

source

pub fn resolve(self) -> Option<T>
where T: MaybeHasId + JsCast,

Resolves this ID into an object, assuming the type T is the correct type of object that this ID refers to. If the ID has been converted to an invalid type, using the returned object in a way not valid for its type will cause a panic.

Will return None if this object no longer exists, or is in a room we don’t have vision for.

Trait Implementations§

source§

impl<T> Clone for ObjectId<T>

source§

fn clone(&self) -> ObjectId<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for ObjectId<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, T> Deserialize<'de> for ObjectId<T>

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> Display for ObjectId<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> From<ObjectId<T>> for ArrayString<24>

source§

fn from(id: ObjectId<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<ObjectId<T>> for RawObjectId

source§

fn from(id: ObjectId<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<ObjectId<T>> for String

source§

fn from(id: ObjectId<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<ObjectId<T>> for u128

source§

fn from(id: ObjectId<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<RawObjectId> for ObjectId<T>

source§

fn from(raw: RawObjectId) -> Self

Converts to this type from the input type.
source§

impl<T> From<u128> for ObjectId<T>

source§

fn from(packed: u128) -> Self

Converts to this type from the input type.
source§

impl<T> FromStr for ObjectId<T>

§

type Err = RawObjectIdParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<T> Hash for ObjectId<T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T> Ord for ObjectId<T>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T> PartialEq<ObjectId<T>> for RawObjectId

source§

fn eq(&self, other: &ObjectId<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialEq<RawObjectId> for ObjectId<T>

source§

fn eq(&self, other: &RawObjectId) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialEq for ObjectId<T>

source§

fn eq(&self, o: &ObjectId<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialOrd<ObjectId<T>> for RawObjectId

source§

fn partial_cmp(&self, other: &ObjectId<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T> PartialOrd<RawObjectId> for ObjectId<T>

source§

fn partial_cmp(&self, other: &RawObjectId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T> PartialOrd for ObjectId<T>

source§

fn partial_cmp(&self, other: &ObjectId<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T> Serialize for ObjectId<T>

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T> Copy for ObjectId<T>

source§

impl<T> Eq for ObjectId<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for ObjectId<T>
where T: RefUnwindSafe,

§

impl<T> Send for ObjectId<T>
where T: Send,

§

impl<T> Sync for ObjectId<T>
where T: Sync,

§

impl<T> Unpin for ObjectId<T>
where T: Unpin,

§

impl<T> UnwindSafe for ObjectId<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,