Skip to content

Instantly share code, notes, and snippets.

The Rust Playground rust-play

Block or report user

Report or block rust-play

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
/*
input: [i32 i64][ A B ]
prev_expanded: []
next_expanded: []
input: [i64][ A B ]
prev_expanded: []
next_expanded: [[i32]]
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
const NAME: &[u8] = b"abcde";
fn main() {
// just setting up an example value.
let mut extension_name: [u8; 256] = unsafe { core::mem::zeroed() };
extension_name[..5].copy_from_slice(NAME);
let name: String = String::from_utf8_lossy(
&extension_name
.iter()
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
fn main() {
let mut v: Vec<u8> = Vec::new();
let one: &[u8] = "hello".as_bytes();
let two: &[u8] = "world".as_bytes();
v.extend(one);
v.extend(two);
dbg!(v);
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
#![forbid(unsafe_code)]
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
/// Poll a future once.
struct PollOnce<'a, F>(Option<&'a mut F>);
impl<'a, F> PollOnce<'a, F> {
pub fn new(future: &'a mut F) -> Self {
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
#![forbid(unsafe_code)]
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
/// Poll a future once.
struct PollOnce<'a, F>(Option<&'a mut F>);
impl<'a, F> PollOnce<'a, F> {
pub fn new(future: &'a mut F) -> Self {
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
/// Poll a future once.
struct PollOnce<'a, F>(Option<&'a mut F>);
impl<'a, F> PollOnce<'a, F> {
pub fn new(future: &'a mut F) -> Self {
Self(Some(future))
}
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
impl From<&Message> for Vec<u8> {
fn from(m: &Message) -> Vec<u8> {
let mut v: Vec<u8> = Vec::new();
let bs: Vec<u8> = (&m.prelude).into();
v.append(&mut bs.clone());
let crc = m.prelude.crc();
let mut val_bs: Vec<u8> = u32::to_be_bytes(crc).to_vec();
v.append(&mut val_bs);
let mut bs: Vec<u8> = (&m.data).into();
v.append(&mut bs);
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
fn cast_slice(slice: &[i8]) -> &[u8] {
unsafe { std::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
}
fn main() {
let array: [i8; 256] = [0; 256];
let slice: &[u8] = cast_slice(&array[..]);
println!("{:?}", slice);
}
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
/// Poll a future once.
struct PollOnce<'a, F>(Option<&'a mut F>);
impl<'a, F: Future + Unpin> Future for PollOnce<'a, F> {
type Output = Option<F::Output>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@rust-play
rust-play / playground.rs
Created Oct 18, 2019
Code shared from the Rust Playground
View playground.rs
use std::future::Future;
use std::task::{Context, Poll};
use std::pin::Pin;
/// Poll a future once.
struct PollOnce<'a, F>(Option<&'a mut F>);
impl<'a, F: Future + Unpin> Future for PollOnce<'a, F> {
type Output = Option<F::Output>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
You can’t perform that action at this time.