
Let mut f = File::create("/tmp/output.bin").unwrap() īincode::serialize_into(&mut f, &a).unwrap()

Encode to something implementing `Write` Heres a working example based on the question: unsafe fn any_as_u8_slice(p: &T) -> & Note that the question asks about converting, this example creates a read-only slice, so has the advantage of not needing to copy the memory. In the example below there there is a reusable function called any_as_u8_slice instead of convert_struct, since this is a utility to wrap cast and slice creation.

There's a little more ceremony used to parse the arguments provided by the user, but it's not super relevant to this post.A correctly sized struct as zero-copied bytes can be done using stdlib and a generic function. Then we pattern match on the result and write an appropriate message to the console. Port 9 is useful because it targets the discard protocol and we don't expect to get a response anyway. When enabled, this socket is allowed to send packets to a broadcast address.įinally we ask the socket to send the &magic_packet to the address 255.255.255.255:9. We create a UDP socket bound to the given address, then we set_broadcast(true), which (from the docs) We could have done this ourselves, but the hex library helps out with error handling fn string_mac_to_bytes ( incoming : & str ) -> Vec ", msg ) That can be achieved fairly simply in Rust with an iterator and using an external library for parsing hex values. The MAC address will be supplied in the common 00:11:22:33:44::55 style and will be converted to bytes internally. The minimal UX I want to provide is a command line application called wol which will take one argument, the MAC address of the target. We're going to send this packet via UDP broadcast across the local network. The final payload we're looking to produce looks like this. Given an example MAC address of 00:1A:BB:CD:EE:61 we can convert that to bytes too. The magic packet is a broadcast frame which contains 6 bytes each of which will be the number 255, followed by the MAC address of the target machine, repeated 16 times.Ĭommonly, 255 would be referred to as FF in hexadecimal, we can convert this to bytes. The way we trigger the behaviour is to send a 'magic packet'. Generally speaking it only working on local networks, which is fine for my purposes. Happily, there's a thing called Wake On LAN which allows you to send a message to hardware that supports this feature. I eventually ran into an issue where I wanted to be able to remotely wake the Mac Mini and started reading up about Wake on LAN and of course, as a nerd, I wanted to build my own tool. Moving a large chunk of that workload to a different machine on my local network hugely extends the time I can spend working outside without having to run extension cords everywhere. I do a lot of frontend development and with all the build tooling that comes along with it, I find my laptop only allows for around 1.5 hours of heavy frontend development when on battery. Using VS-Code Remote has allowed me to remain very productive whilst experimenting with the new platform.
#RUST CONVERT MAC ADDRESS TO U8 FULL#
I recently invested in a Mac Mini M1 as a secondary machine and have been enjoying using it with VS-Code Remote.Īs a developer, the Apple Silicon experience isn't quite ready for full time use while minor incompatibilities are still getting ironed out. Categories Tags Resume Wake on LAN in Rust DecemRead time: 5 minute(s) 841 words
