1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
use std::{
fmt::{self, Write},
str::FromStr,
};
use arrayvec::ArrayString;
use serde::{Deserialize, Serialize};
use stdweb::{Reference, UnsafeTypedArray};
use super::errors::RawObjectIdParseError;
use crate::{
traits::{TryFrom, TryInto},
ConversionError,
};
const MAX_PACKED_VAL: u128 = (1 << (32 * 3)) - 1;
/// 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.
///
/// [`BTreeMap`]: std::collections::BTreeMap
/// [`Ord`]: std::cmp::Ord
/// [`PartialOrd`]: std::cmp::PartialOrd
/// [`ObjectId`]: super::ObjectId
#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(transparent)]
pub struct RawObjectId {
packed: [u32; 3],
}
impl fmt::Debug for RawObjectId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RawObjectId")
.field("packed", &self.packed)
.field("real", &self.to_array_string())
.finish()
}
}
impl fmt::Display for RawObjectId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:x}", self.to_u128())
}
}
impl FromStr for RawObjectId {
type Err = RawObjectIdParseError;
fn from_str(s: &str) -> Result<Self, RawObjectIdParseError> {
let u128_val = u128::from_str_radix(s, 16)?;
Self::try_from(u128_val)
}
}
impl TryFrom<u128> for RawObjectId {
type Error = 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.
fn try_from(val: u128) -> Result<Self, RawObjectIdParseError> {
if val > MAX_PACKED_VAL {
return Err(RawObjectIdParseError::value_too_large(val));
}
// if the endianness is right, then I think this should optimize down to a
// transmute. If it isn't, then it should be pretty efficient anyways and will
// still be _correct_.
let as_array = [
((val >> 64) & 0xFFFF_FFFF) as u32,
((val >> 32) & 0xFFFF_FFFF) as u32,
(val & 0xFFFF_FFFF) as u32,
];
Ok(Self::from_packed(as_array))
}
}
impl RawObjectId {
/// 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.
pub fn from_packed(packed: [u32; 3]) -> Self {
RawObjectId { packed }
}
/// 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
///
/// ```no_run
/// 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);
/// ```
pub fn from_packed_js_val(packed_val: Reference) -> Result<Self, ConversionError> {
let mut packed = [0u32; 3];
// TODO: make this more efficient, once we get mutable UnsafeTypedArrays.
// See https://github.com/koute/stdweb/issues/360.
packed[0] = js! {return @{&packed_val}[0]}.try_into()?;
packed[1] = js! {return @{&packed_val}[1]}.try_into()?;
packed[2] = js! {return @{&packed_val}[2]}.try_into()?;
Ok(Self::from_packed(packed))
}
/// 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.
pub fn to_u128(self) -> u128 {
((self.packed[0] as u128) << 64)
| ((self.packed[1] as u128) << 32)
| (self.packed[2] as u128)
}
/// Internal function which trims off leading zero integers.
fn non_zero_packed_ints(&self) -> &[u32] {
for i in 0..3 {
if self.packed[i] != 0 {
return &self.packed[i..3];
}
}
// fallback to static zero-sized slice if we have no non-zero integers...
&[]
}
/// 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
///
/// ```no_run
/// 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);
/// }
/// ```
pub fn to_array_string(&self) -> ArrayString<[u8; 24]> {
let mut res = ArrayString::new();
write!(res, "{}", self).expect("expected formatting into a fixed-sized buffer to succeed");
res
}
/// 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
///
/// ```no_run
/// 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);
/// }
/// ```
pub unsafe fn unsafe_as_uploaded(&self) -> UnsafeTypedArray<'_, u32> {
UnsafeTypedArray::new(self.non_zero_packed_ints())
}
}
impl From<RawObjectId> for ArrayString<[u8; 24]> {
fn from(id: RawObjectId) -> Self {
id.to_array_string()
}
}
impl From<RawObjectId> for String {
fn from(id: RawObjectId) -> Self {
id.to_string()
}
}
impl From<RawObjectId> for u128 {
fn from(id: RawObjectId) -> Self {
id.to_u128()
}
}
impl From<RawObjectId> for [u32; 3] {
fn from(id: RawObjectId) -> Self {
id.packed
}
}
impl From<[u32; 3]> for RawObjectId {
fn from(packed: [u32; 3]) -> Self {
Self::from_packed(packed)
}
}
#[cfg(test)]
mod test {
use super::RawObjectId;
#[cfg(target_arch = "wasm32")]
use crate::macros::*;
use crate::traits::TryInto;
const TEST_IDS: &[&str] = &[
"bc03381d32f6790",
"1",
"ffffffffffffffffffffffff",
"100000000000000000000000",
"10000000000000000",
"1000000000000000",
"100000000",
"10000000",
];
#[test]
fn rust_display_rust_fromstr_roundtrip() {
for id in TEST_IDS {
let parsed: RawObjectId = id.parse().unwrap();
assert_eq!(&*parsed.to_string(), *id);
}
}
#[test]
fn rust_to_array_string_rust_fromstr_roundtrip() {
for id in TEST_IDS {
let parsed: RawObjectId = id.parse().unwrap();
assert_eq!(&*parsed.to_array_string(), *id);
}
}
#[test]
fn rust_to_u128_from_u128_roundtrip() {
for id in TEST_IDS {
let parsed: RawObjectId = id.parse().unwrap();
let int = parsed.to_u128();
let reparsed: RawObjectId = int.try_into().unwrap();
assert_eq!(parsed, reparsed);
assert_eq!(reparsed.to_string(), *id);
}
}
#[test]
fn large_values_do_not_parse() {
let large_ids = &[
"1000000000000000000000000".to_owned(),
format!("{:x}", u128::max_value()),
];
for id in large_ids {
let res: Result<RawObjectId, _> = id.parse();
assert!(res.is_err());
}
}
#[test]
#[cfg(target_arch = "wasm32")]
fn js_format_rust_fromstr_roundtrip() {
for id in TEST_IDS {
let parsed: RawObjectId = id.parse().unwrap();
let array_view = unsafe { parsed.unsafe_as_uploaded() };
let js_produced_string: String = js_unwrap!(object_id_from_packed(@{array_view}));
let reparsed: RawObjectId = js_produced_string
.parse()
.expect("expected to successfully reparse object id");
assert_eq!(parsed, reparsed);
}
}
#[test]
#[cfg(target_arch = "wasm32")]
fn rust_display_js_parse_roundtrip() {
for id in TEST_IDS {
let parsed: RawObjectId = id.parse().unwrap();
let string = parsed.to_array_string();
let js_produced_vals = js_unwrap!(object_id_to_packed(@{&*string}));
let recreated = RawObjectId::from_packed_js_val(js_produced_vals).unwrap();
assert_eq!(parsed, recreated);
}
}
}