Struct screeps::local::RawObjectId
source · [−]pub struct RawObjectId { /* private fields */ }
Expand description
Represents an Object ID using a packed 12-byte representation
Each object id in screeps is represented by a Mongo GUID, which, while not guaranteed, is unlikely to change. This takes advantage of that by storing a packed representation.
To convert to a String in JavaScript, either use
RawObjectId::to_array_string
, or RawObjectId::unsafe_as_uploaded
.
See method documentation for more information.
Ordering
To facilitate use as a key in a BTreeMap
or other similar data
structures, ObjectId
implements PartialOrd
and Ord
.
RawObjectId
’s are ordered by the corresponding order of their underlying
byte values. See ObjectId
documentation for more information.
Implementations
sourceimpl RawObjectId
impl RawObjectId
sourcepub fn from_packed(packed: [u32; 3]) -> Self
pub fn from_packed(packed: [u32; 3]) -> 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.
sourcepub fn from_packed_js_val(
packed_val: Reference
) -> Result<Self, ConversionError>
pub fn from_packed_js_val(
packed_val: Reference
) -> Result<Self, ConversionError>
Creates an object ID from a packed representation stored in JavaScript.
The input must be a reference to a length-3 array of integers.
Recommended to be used with the object_id_to_packed
JavaScript utility
function, which takes in a string and returns the array of three
integers that this function expects.
Example
use screeps::{prelude::*, traits::TryInto, RawObjectId};
use stdweb::js;
let packed_obj_id = (js! {
let creep = _.sample(Game.creeps);
return object_id_to_packed(creep.id);
})
.try_into()
.unwrap();
let parsed = RawObjectId::from_packed_js_val(packed_obj_id).unwrap();
println!("found creep with id {}", parsed);
sourcepub fn to_u128(self) -> u128
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.
sourcepub fn to_array_string(&self) -> ArrayString<[u8; 24]>
pub fn to_array_string(&self) -> ArrayString<[u8; 24]>
Formats this object ID as a string on the stack.
This is equivalent to ToString::to_string
, but involves no
allocation.
To use the produced string in stdweb, use &*
to convert it to a string
slice.
This is less efficient than RawObjectId::unsafe_as_uploaded
, but
easier to get right.
Example
use screeps::{prelude::*, RawObjectId};
use stdweb::js;
let object_id: RawObjectId = screeps::game::creeps::values()[0].untyped_id();
let str_repr = object_id.to_array_string();
js! {
let id = @{&*str_repr};
console.log("we have a creep with the id " + id);
}
sourcepub unsafe fn unsafe_as_uploaded(&self) -> UnsafeTypedArray<'_, u32>
pub unsafe fn unsafe_as_uploaded(&self) -> UnsafeTypedArray<'_, u32>
Creates an array accessible from JavaScript which represents part of this object id’s packed representation.
Specifically, the resulting array will contain the first non-zero number
in this object id, and all following numbers. This allows for a more
efficient object_id_from_packed
implementation.
Safety
This is highly unsafe.
This creates an UnsafeTypedArray
and does not use it in JS, so the
restrictions from UnsafeTypedArray
apply. When you call into
JavaScript using it, you must “use” it immediately before calling into
any Rust code whatsoever.
There are other safety concerns as well, but all deriving from
UnsafeTypedArray
. See UnsafeTypedArray
.
Example
use screeps::{prelude::*, RawObjectId};
use stdweb::js;
let object_id: RawObjectId = screeps::game::creeps::values()[0].untyped_id();
let array_view = unsafe { object_id.unsafe_as_uploaded() };
js! {
let id = object_id_from_packed(@{array_view});
console.log("we have a creep with the id " + id);
}
Trait Implementations
sourceimpl Clone for RawObjectId
impl Clone for RawObjectId
sourcefn clone(&self) -> RawObjectId
fn clone(&self) -> RawObjectId
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for RawObjectId
impl Debug for RawObjectId
sourceimpl<'de> Deserialize<'de> for RawObjectId
impl<'de> Deserialize<'de> for RawObjectId
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Display for RawObjectId
impl Display for RawObjectId
sourceimpl<T> From<ObjectId<T>> for RawObjectId
impl<T> From<ObjectId<T>> for RawObjectId
sourceimpl From<RawObjectId> for [u32; 3]
impl From<RawObjectId> for [u32; 3]
sourcefn from(id: RawObjectId) -> Self
fn from(id: RawObjectId) -> Self
Converts to this type from the input type.
sourceimpl From<RawObjectId> for ArrayString<[u8; 24]>
impl From<RawObjectId> for ArrayString<[u8; 24]>
sourcefn from(id: RawObjectId) -> Self
fn from(id: RawObjectId) -> Self
Converts to this type from the input type.
sourceimpl<T> From<RawObjectId> for ObjectId<T>
impl<T> From<RawObjectId> for ObjectId<T>
sourcefn from(raw: RawObjectId) -> Self
fn from(raw: RawObjectId) -> Self
Converts to this type from the input type.
sourceimpl From<RawObjectId> for String
impl From<RawObjectId> for String
sourcefn from(id: RawObjectId) -> Self
fn from(id: RawObjectId) -> Self
Converts to this type from the input type.
sourceimpl From<RawObjectId> for u128
impl From<RawObjectId> for u128
sourcefn from(id: RawObjectId) -> Self
fn from(id: RawObjectId) -> Self
Converts to this type from the input type.
sourceimpl FromStr for RawObjectId
impl FromStr for RawObjectId
type Err = RawObjectIdParseError
type Err = RawObjectIdParseError
The associated error which can be returned from parsing.
sourceimpl Hash for RawObjectId
impl Hash for RawObjectId
sourceimpl Ord for RawObjectId
impl Ord for RawObjectId
sourcefn cmp(&self, other: &RawObjectId) -> Ordering
fn cmp(&self, other: &RawObjectId) -> Ordering
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<T> PartialEq<ObjectId<T>> for RawObjectId
impl<T> PartialEq<ObjectId<T>> for RawObjectId
sourceimpl<T> PartialEq<RawObjectId> for ObjectId<T>
impl<T> PartialEq<RawObjectId> for ObjectId<T>
sourcefn eq(&self, other: &RawObjectId) -> bool
fn eq(&self, other: &RawObjectId) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourceimpl PartialEq<RawObjectId> for RawObjectId
impl PartialEq<RawObjectId> for RawObjectId
sourcefn eq(&self, other: &RawObjectId) -> bool
fn eq(&self, other: &RawObjectId) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourceimpl<T> PartialOrd<ObjectId<T>> for RawObjectId
impl<T> PartialOrd<ObjectId<T>> for RawObjectId
sourcefn partial_cmp(&self, other: &ObjectId<T>) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl<T> PartialOrd<RawObjectId> for ObjectId<T>
impl<T> PartialOrd<RawObjectId> for ObjectId<T>
sourcefn partial_cmp(&self, other: &RawObjectId) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl PartialOrd<RawObjectId> for RawObjectId
impl PartialOrd<RawObjectId> for RawObjectId
sourcefn partial_cmp(&self, other: &RawObjectId) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl Serialize for RawObjectId
impl Serialize for RawObjectId
sourceimpl TryFrom<u128> for RawObjectId
impl TryFrom<u128> for RawObjectId
sourcefn try_from(val: u128) -> Result<Self, RawObjectIdParseError>
fn try_from(val: u128) -> Result<Self, RawObjectIdParseError>
Creates an object ID from its binary representation as a u128
number.
Errors
This will error if the given value is greater than 2^96 - 1
, the
maximum number storable in a 96-bit integer.
type Error = RawObjectIdParseError
type Error = RawObjectIdParseError
The type returned in the event of a conversion error.
impl Copy for RawObjectId
impl Eq for RawObjectId
impl StructuralEq for RawObjectId
impl StructuralPartialEq for RawObjectId
Auto Trait Implementations
impl RefUnwindSafe for RawObjectId
impl Send for RawObjectId
impl Sync for RawObjectId
impl Unpin for RawObjectId
impl UnwindSafe for RawObjectId
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T, U> IntoExpectedType<U> for Twhere
U: FromExpectedType<T>,
impl<T, U> IntoExpectedType<U> for Twhere
U: FromExpectedType<T>,
sourcefn into_expected_type(self) -> Result<U, ConversionError>
fn into_expected_type(self) -> Result<U, ConversionError>
Casts this value as the target type, making the assumption that the types are correct. Read more